当NG-模型控制器初始化Angularjs选择NG变化不会触发 [英] Angularjs select ng-change is not triggered when ng-model is initialized in controller
问题描述
我有我的选择,像这样
<select ng-model="id_select" ng-options="fide.id as fide.name for fide in fides" ng-change="loadInfo()">
<option value="" selected disabled>Seleccionar</option>
</select>
在我的控制器我有这个
And in my controller I have this
RestService.getFides().then(function(fides){
$scope.id_select = fides;
if(typeof $rootScope.selected_id !== 'undefined')
{
$scope.id_select = $rootScope.selected_id
}
});
$scope.loadInfo = function() {
alert("triggered!!");
}
我的选择是加载预期与惹人但是当我尝试设置我的$ rootScope价值NG-切换功能loadInfo()选择值不会被触发,但是当我在视图中选择它工作正常。
My select is loading as expected with fides but when I try to set my select value with $rootScope's value ng-change function loadInfo() is not triggered but when I select in my view it works fine.
的我在做什么任何想法错了?
Any ideas of what am I doing wrong?
感谢您所有您的帮助:)
Thank you all in advance for your help :)
推荐答案
从文档 ngChange
:
评估前给予pression 当用户改变输入。这位前pression立即进行评估,不同的是JavaScript的onchange事件仅触发在变化的结束(通常,当用户离开表单元素或presses返回键)。 当值的变化是从模型来这位前pression不计算
Evaluate the given expression when the user changes the input. The expression is evaluated immediately, unlike the JavaScript onchange event which only triggers at the end of a change (usually, when the user leaves the form element or presses the return key). The expression is not evaluated when the value change is coming from the model.
您需要打电话给你的事件处理程序,如果你手工编程正在改变模型:
You need to call your event handler manually if you are changing the model programatically:
if (typeof $rootScope.selected_id !== 'undefined') {
$scope.id_select = $rootScope.selected_id;
$scope.loadInfo();
}
这篇关于当NG-模型控制器初始化Angularjs选择NG变化不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!