带有动态数据的 ng-init 不起作用
下面是我的 html,其中包含 div 中的选择字段。
<div class="form-group">
<div class="col-lg-4">
<select name="trader" class="form-control" id="select" ng-model="$root.customerDetails.traderType"
ng-init="$ctrl.initi()" ng-options="traderTypeObj.description for traderTypeObj in traderTypes
track by traderTypeObj.type" >
</select>
</div>
现在我需要默认显示从服务获得的选择字段中的第一个选项。所以我使用 ng-init。但它不起作用。所以我尝试从 ng-init 调用函数。
如果我设置此$rootScope.customerDetails.traderType=$scope.traderTypes[1];
,默认情况下将显示第一个选项。我通过使用一个按钮并设置 onclick 和它的工作。所以现在我尝试使用 ng-init 并使用 $scope.$apply() 来调用该函数
this.initi=function(){
$rootScope.customerDetails.traderType = $scope.traderTypes[1];
$scope.$apply();
};
第一次加载时,html 会出现错误,第二次加载时会出现错误:[$rootScope:inprog] $digest 已在进行中
问题是我在调用 ajax 服务之前调用该值......
我怎样才能设置该选项..请有人帮助我
最佳答案
试试这个,
this.initi=function(){
$timeout(function() {
$rootScope.customerDetails.traderType = $scope.traderTypes[1];
$scope.$apply();
})
};
关于javascript - 面对动态数据的 ng-init 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838342/