ng:onchange on a select dropdown, slight delay to reflect new value?

12,994 views
Skip to first unread message

jae lee

unread,
Dec 6, 2011, 1:17:19 PM12/6/11
to AngularJS
Hi guys

I have a select dropdown that I want to get the current selected value
via the ng:change:

<select class="small" name="invoice.invoice_status"
ng:change="setStatus(invoice.invoice_status)" ng:options="s.id as
s.status_name for s in invoice_statuses"></select>

but upon change, I get the value it was selected before the change
within the setStatus(invoice_status).

so it seems, there is a delay between when the new value is registered
to the model in ng:change().

how can I just get the current new value of the select change event?
without resorting to jquery/etc...

thanks,
jae

Mårten Dolk

unread,
Dec 6, 2011, 2:04:06 PM12/6/11
to ang...@googlegroups.com
I think ng;change has been deprecated, the preferred way is to set a
$watch on invoice.invouce_status.

http://docs.angularjs.org/#!/api/angular.scope.$watch

Hope this helps.

/Mårten

2011/12/6 jae lee <furi...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>

knrdk

unread,
Dec 6, 2011, 9:59:02 PM12/6/11
to AngularJS
How would that work when iterating through an array?

Mårten Dolk

unread,
Dec 7, 2011, 4:46:26 AM12/7/11
to ang...@googlegroups.com
Do you mean if you have selects in ng:repeat?

In 0.10.x you can watch an array. I don't think this was possible in
0.9.x (without resorting to watching a json-formated string of the
array).

Here is an example:
http://jsfiddle.net/aq6SY/

Not sure why I get the entire scope in the watch callback though... bug?

/Mårten

2011/12/7 knrdk <klun...@gmail.com>:


> How would that work when iterating through an array?
>

knrdk

unread,
Dec 7, 2011, 12:44:03 PM12/7/11
to AngularJS
Oh neat. Can that be used to watch individual values of an array?

For example, I wouldn't want to watch all of scope.records, but just
scope.records[].status.

Mårten Dolk

unread,
Dec 8, 2011, 5:58:36 AM12/8/11
to ang...@googlegroups.com
Mystery of why I get scope solved... a changed API :-) It is even documented
http://docs-next.angularjs.org/api/angular.scope.$watch

As to watch just some property of objects in array; The watch
expression can be any function, I guess something like this would
work.

scope.$watch(function() {
var statuses = [];
angular.forEach( scope.records, function(record) {
statuses.push(record.status);
});
return statuses;
}, onChangeFn);

/mårten


2011/12/7 knrdk <klun...@gmail.com>:


> Oh neat. Can that be used to watch individual values of an array?
>
> For example, I wouldn't want to watch all of scope.records, but just
> scope.records[].status.
>

Vojta Jina

unread,
Dec 9, 2011, 5:31:01 PM12/9/11
to ang...@googlegroups.com
Thanks for answering Marten.

Yep, the first argument of the watch is scope... but to be super confusing, we will revert this :-D, because we are going to separate scope / controllers. And then, it makes no sense to pass scope into watch function, as you don't need it...


V.

Mårten Dolk

unread,
Dec 10, 2011, 5:57:35 AM12/10/11
to ang...@googlegroups.com
Nice! I like that change very much :-)

2011/12/9 Vojta Jina <vojta...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/krYjduMPKCcJ.

Misko Hevery

unread,
Dec 10, 2011, 1:53:24 PM12/10/11
to ang...@googlegroups.com
NG:change is not deprecated, and it is different form $watch. $watch means notify when the value changes. ng:change means notify when the user causes the change, and that is not quite the same as $watch.

ng:change should fire after the bindings, if that is not the case then it is a bug. Could you create jsfiddle demonstrating the issue?

Mårten Dolk

unread,
Dec 11, 2011, 8:22:38 AM12/11/11
to ang...@googlegroups.com
Ah, you're of course right, ng:change is still there... It was
documented as its own directive in 0.9 but is now documented in the
input widgets in 0.10. I can see both useful.

Sorry for causing any confusion.

/Mårten

2011/12/10 Misko Hevery <mi...@hevery.com>:

Mårten Dolk

unread,
Dec 11, 2011, 8:37:36 AM12/11/11
to ang...@googlegroups.com
ng:select widget is missing ng:change (and ng:model) in the docs-next
documentation, input and textarea have them...

http://docs-next.angularjs.org/api/angular.widget.textarea
http://docs-next.angularjs.org/api/angular.widget.input
http://docs-next.angularjs.org/api/angular.widget.select

/mårten

2011/12/11 Mårten Dolk <marte...@gmail.com>:

rudebear

unread,
Apr 18, 2013, 10:16:55 PM4/18/13
to ang...@googlegroups.com
Hi Misko,

For standard select box changes that change other parts of a form according to what was selected, would it be best practice to use $watch or ng-changed?

Thanks
Reply all
Reply to author
Forward
0 new messages