admin管理员组文章数量:1287487
Below is a pretty straightforward setup for a form drop down list. However, the on-change
event refuses to fire... unless it's changed to ng-change
.
This had me stuck for about an hour, since we use the same setup elsewhere in our site (i.e. model property/list/on-change
directive) without fail.
I'm wondering what the difference is between on-change
and ng-change
and why one works in this instance and the other doesn't?
View
<select id="grArchive"
name="grArchive"
class="form-control"
options="userList"
ng-model="selectedUser"
ng-options="user as user.name for user in userList"
on-change="showSelected()">
</select>
Controller
scope.selectedUser = {};
scope.userList = [];
scope.showSelected = function(){
scope.columns = addColumns;
};
var loadUserList = function(data){
scope.userList = $.map(data, function(item){
return {id: item.id, name: item.firstName + ' ' + item.lastName};
});
}
Just in case: the user drop down populates as expected (screen shot below)
Below is a pretty straightforward setup for a form drop down list. However, the on-change
event refuses to fire... unless it's changed to ng-change
.
This had me stuck for about an hour, since we use the same setup elsewhere in our site (i.e. model property/list/on-change
directive) without fail.
I'm wondering what the difference is between on-change
and ng-change
and why one works in this instance and the other doesn't?
View
<select id="grArchive"
name="grArchive"
class="form-control"
options="userList"
ng-model="selectedUser"
ng-options="user as user.name for user in userList"
on-change="showSelected()">
</select>
Controller
scope.selectedUser = {};
scope.userList = [];
scope.showSelected = function(){
scope.columns = addColumns;
};
var loadUserList = function(data){
scope.userList = $.map(data, function(item){
return {id: item.id, name: item.firstName + ' ' + item.lastName};
});
}
Just in case: the user drop down populates as expected (screen shot below)
Share Improve this question edited Jul 14, 2016 at 16:16 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Jul 14, 2016 at 16:00 NealRNealR 10.7k61 gold badges167 silver badges306 bronze badges1 Answer
Reset to default 7ng-change
ng-change
is a directive provided by Angular core API which internally registers an expression to be called when any change happens in $viewValue
of ng-model
variable (here is the code); assign it an expression such as myFunction()
. That provided expression will evaluate inside the underlying controller scope. After calling an expression it runs a digest cycle to make sure bindings are updated on the view. Besides ng-change
there are other directives used for events, like ng-focus
,ng-mousedown
,ng-submit
, ng-blur
, etc. Here is a list of such directives
on-change
It is a way of calling a JavaScript function on change
of input element value. It will search for the function
which is globally available in context (obviously it will not call the function registered in angular controller) and evaluate it.
本文标签: javascriptDifference between onchange and ngchangeStack Overflow
版权声明:本文标题:javascript - Difference between on-change and ng-change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741287071a2370333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论