当ng-options列表被改变时,Ng-init不工作
ng-init not working when the ng-options list is changed
下面的select元素加载并选择控件加载时的第一个选项。我基于另一个下拉列表改变了positionAllowedList。但是ng-init不选择第一项。
<select ng-init="col.positionselected = col.positionAllowedList[0].value" ng-options="pos.value as pos.text for pos in col.positionAllowedList" ng-model="col.positionselected">
</select>
$scope.ChangeBookingIso = function (col) {
$.each(col.bookingIsoList, function (i, item) {
....someLogic to change it is exactly the same structure the json list as before at the first time loaded.....
col.positionAllowedList = positionAllowedList;
})
}
您使用的指令是错误的。ng-init
从未被设计为基于视图上的任意(尽管可能被滥用)表达式进行初始化。ng-init
文档本身说:
ngInit唯一合适的用法是混叠ngRepeat的特殊属性,如下面的演示所示。除了这种情况,你应该使用控制器而不是ngInit来初始化作用域中的值。
所以一般用法是用ng-repeat来混叠它的特殊属性,如$index
, $first
等,例如,当你在子ng-repeat中使用嵌套的ng-repeat
时,你可以通过ng-init
设置的别名访问父ng-repeat的$index
,而不使用$parent.$index
, $parent.$parent...$index
等。
它也只是一次初始化,如果你看一下ng-init的源代码,它是非常简单和直接的。
var ngInitDirective = ngDirective({
priority: 450,
compile: function() {
return {
pre: function(scope, element, attrs) {
scope.$eval(attrs.ngInit);
}
};
}
});
如果您注意到没有创建watch
以便在每个摘要周期中重新计算表达式。所以它不像你想的那样工作。
所以只要从select
中删除ng-init
,并在控制器本身中设置属性col.positionselected
。
相关文章:
- ng init中的表达式无法使用ng repeat
- Cordova ng路线工作不正常
- AngularJS ng-init does not work
- 使用jqGrid列格式化程序函数使ng-click工作
- ng重复工作但没有表现出表情
- ng src工作不正常,但src工作正常
- ng init未按预期工作
- 在<选择>使用angular ng选项可能使用ng init
- $localStorage array select with ng选项工作不正常
- ng init未进行初始化
- AngularJS:如何在间隔后执行 ng-init
- 在指令之后加载的角度ng-init函数
- ng init与razor表达式
- ng-选项与ng-init无法正常工作
- 设置变量时,Angular Js ng init在ng repeat内部无法正常工作
- Angular JS:控制器不工作.当我尝试使用ng-init时,所有的事情都很完美
- NG-INIT不能正常工作
- 调试通过ng-init进行的双向绑定不工作
- 由ng init预先选择的选项不'无法在ngOptions|AngularJS中工作
- 当ng-options列表被改变时,Ng-init不工作