gpt4 book ai didi

当ng-model在 Controller 中初始化时,Angularjs select ng-change不会被触发

转载 作者:行者123 更新时间:2023-12-02 22:25:28 25 4
gpt4 key购买 nike

我的选择是这样的

<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>

在我的 Controller 中我有这个

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!!");
}

我的选择正在按 fides 的预期加载,但是当我尝试使用 $rootScope 的值 ng-change 函数 loadInfo() 设置我的选择值时,不会触发,但当我在 View 中选择时,它工作正常。

我做错了什么有什么想法吗?

提前感谢大家的帮助:)

最佳答案

来自documentation for ngChange :

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.

如果您要以编程方式更改模型,则需要手动调用事件处理程序:

if (typeof $rootScope.selected_id !== 'undefined') {
$scope.id_select = $rootScope.selected_id;
$scope.loadInfo();
}

关于当ng-model在 Controller 中初始化时,Angularjs select ng-change不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24579186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com