0

Can you help me in passing the variable values from one method to another method

example:

myAction2 : function(component, event, helper) {

         var payload = event.getParams().response;
                var newid = payload.id;

handleSave: function(cmp, event, helper) {
   i want to access newid 
}

both methods are called at different instances in the component.

  • 1
    What have you tried that isn't working for you? It appears you have defined an event. Please add your code that defines the Event. What are you pulling from the Component? – crmprogdev Oct 9 '18 at 12:53
  • i am not using any events ,in the first action i am creating a record and storing the created recordid value in newid,i need to pass this newid along with some other fields in the next method handlesave ,i am not sure whether it is possible in lightning – anil Oct 9 '18 at 13:00
  • Is this your controller or helper? – Ranga Oct 9 '18 at 13:02
  • its my controller , – anil Oct 9 '18 at 13:09
2

You can store newid in an aura attribute and access it from other method.

Add this line to component:

<aura:attribute name="newId" type="Id" />

Add this line to function where you get the id after you get the id

component.set("v.newId",newId);

And add this line to where you want to get and use the id

var newId = component.get("v.newId");
1

There are couple ways of parsing values around in lightning components.

  1. Storing as an attribute. Eg. You can assign the variable value to an attribute in the component and access it from another method. This can be implemented in your helper as well as in your controller.

Eg. Component,

<aura:attribute name="tempId" type="String"/>

Controller/Helper

foo : function(component, event, helper) {
      var newId = 'xxxxxxxxx';
      component.set("v.tempId", newId);

    },

bar: function(component, event, helper) {
   console.log(component.get("v.tempId));
}
  1. Passing as a parameter to a method.

This can be done within your helper only.

foo : function(component, helper) {
  var newId = 'xxxxxxxxx';
  helper.bar(component, newId);

},

bar: function(component, tempId) {
   console.log(tempId);
}

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.