We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

snChoiceList Directive snOnChange issue

Thomas Wright
Giga Expert

Hi All,

Need some Service Portal widget expertise please. I'm having trouble getting the value out of a sn-choice-list field with an onchange. Here's my HTML:

<sn-choice-list

        sn-on-change="c.valueSelected(attribute)"

        sn-model="attribute.value"

        sn-items="attribute.choices"

        sn-value-field="value"

        sn-text-field="display">

</sn-choice-list>

And my client script:

c.valueSelected = function(attribute) {

        alert(attribute.value);

};

alert(attribute.value) is giving me the oldValue rather than newValue - guessing it's some asynchronous thing. Anyone know how to get the newValue directly on a change?

11 REPLIES 11

Dawid K1
Tera Contributor

I dig around this problem, as I could not afford to use either setTimeout or $watch and I found another way to access newValue in the onChange function. In short, you just add "selectedValue" argument to your function definition and also to arguments in sn-on-change.  

In the end it would look like 

<sn-choice-list

        sn-on-change="c.valueSelected([otherArguments] , selectedValue)"

        ...>

</sn-choice-list>

c.valueSelected = function([otherArguments] , selectedValue) {

        alert(selectedValue);

};

This is due to the way snChoiceList is implemented, and selectedValue is local containing passed to the execution of assigned function or something like this (sorry I do not know AngularJS very well yet 😛

Hope this helps someone in the future 🙂  

 

anubha
Kilo Contributor

Hiii

If I want to bring [otherArguments]  also in the client side.

then how should I code?