0

I have the following line in my component:

<force:inputField aura:id="locationField" value="{!v.newCase.Location__c}" change="{!c.addressChanged}"/>

which creates an address selection field. When the user changes the address the "change" event is supposed to fire the following code:

addressChanged : function(cmp, evt){
    alert("address changed");
},

but it does not. I've found some old questions that are similar here, here and here but none of them have suitable/working answers - is this event totally broken or is there a working hack?

Note: I'd comment on the above threads to ask the question but I'm in the usual StackOverflow reputation hell - don't want to post a duplicate question but can't comment to ask without posting questions to build reputation...

3
  • Your code seems to be fine. There must be something else that is preventing to call that method. Post your controller and component code. Commented Mar 3, 2017 at 12:28
  • Did you find the solution for this @Simon? Commented Mar 6, 2017 at 21:22
  • @the_phantom couldn't get it to work this way. Instead I set up an event to fire when {!v.newCase.Location__c} changed.
    – Simon
    Commented Mar 7, 2017 at 12:14

2 Answers 2

2

This is not working in Lookup case.. Try change Handler to handle its value..

Here is some code example.

In Component

 <aura:handler name="change" value="{!v.newCase.Location__c}" action="{!c.addressChanged}"/>

And In Controller:

addressChanged: function(cmp, evt, helper) {
        var x = cmp.get("v.newCase.Location__c")
        if(x && x !== 'MALFORMED_ID'){
            alert(x)
        }

    },
0

The typical pattern for this is to define an attribute (the model), bind the input field to the attribute, and set a change handler on the attribute:

<aura:attribute name="searchKey" type="String" default=""/>

<aura:handler name="change" value="{!v.searchKey}" action="{!c.onSearch}"/>

<lightning:input type="text" label="Search:" value="{!v.searchKey}"/>

This is considered a more robust approach because it also supports situations where the searchKey is changed in other ways (not only through the input field). For example if you want to reset the search field, all you have to do is component.set("v.searchKey", "") and the handler will be called automatically.

1
  • Yeah this is how I ended up doing it, but is this because force:inputField 'change' is broken or just difficult to fire consistently?
    – Simon
    Commented Mar 10, 2017 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .