下面的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-repeat的$index
由ng-init
设置,而不使用$parent.$index
,$parent.$parent...$index
等。
同样,这也是一次初始化,如果您查看source for ng-init,它非常简单直接。
var ngInitDirective = ngDirective({
priority: 450,
compile: function() {
return {
pre: function(scope, element, attrs) {
scope.$eval(attrs.ngInit);
}
};
}
});
如果您发现没有创建
watch
以便在每个摘要循环期间重新计算该表达式。因此,毫不奇怪,它不按您的预期工作。因此,只需从
ng-init
中删除select
,然后在控制器本身内设置属性col.positionselected
。