1

It is possible to create attribute as global, I mean that attribute can be get & set in any lightning component or can we set the parent component attributes?

  • yes you can use global,private access specifiers in lightning.Please explain a bit briefly if you have any other questions. – Pavan tej Nov 11 '16 at 7:03
  • My question is, Is it possible to set the attribute values of outer lightning component from inner lightning component? if yes, then how? – Shilpa Nov 11 '16 at 7:16
2

There are couple ways to handle this

1.Approach 1 involves using a static resource file to store all the global values

Here is how a code in your static resource look like

(function(w){
  "use strict"; //Optional because LC on LockerService active runs in strict mode

  var globalConstants = {
    "variable1":"abc",
    "variable2":"zyz"
  };

  w.globalvalues = globalConstants;

})(window);

And then in lightning component you will use like below

  ltng:require scripts="{!$Resource.TestScript}" afterScriptsLoaded="{!c.afterScriptsLoaded}" />
   <ui:button label="Do job" press="{!c.populateConstants}"/>

The controller

populateConstants : function(cmp){
  globalvalues.variable1; 
  globalvalues.variable2; 
}

Note you can use this on all init handler to load this data

2.Use an attribute in Parent component and then trickle down the attribute in all the components like below

<attribute name="constant" type="String" default="Hello"/>

The child will have similar attribute and you will call like below

<c:childcomponent constant="{!v.constant}"/>
  • Thanks for response, but by using the first approach, it will be difficult if my global variables/attributes are dynamic, Every time I have to update & access. And the second approach is passing values from outer cmp to inner cmp, it will not work vice versa. – Shilpa Nov 14 '16 at 6:06
  • @shilpa your question said global attributes .Please be specific with questions. Global variables in JS means different than one can imagine if events. – Mohith Shrivastava Nov 14 '16 at 12:31
1

I got the solution, By using lightning event we can pass values from any component to any component like from outer component to inner component, inner component to outer component and from sibling component too.

Steps:

  1. Create a lightning event with attributes which you want to pass to cmp on particular event.

  2. Register the lightning event in the component from where you want to pass the values/from where the event is gona happen.

  3. Add handler in component in which you are going to access the parameter

Thanks!

0

See one answer to With Summer 18 and LockerService is there any global storage? The concept is to pass a global object from each outer container to each inner container. This "global" object is shared and can be modified by any component that is given the reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.