【问题标题】:Facing issue with ng-init for dynamic datang-init 面临动态数据的问题
【发布时间】:2017-04-11 19:45:52
【问题描述】:

具有动态数据的 ng-init 不起作用

下面是我在 div 中选择字段的 html。

<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 显示为错误,第二次加载时显示Error: [$rootScope:inprog] $digest already in progress

问题是我在调用 ajax 服务之前调用了值...

如何设置该选项..请有人帮助我

【问题讨论】:

    标签: javascript html angularjs node.js meteor


    【解决方案1】:

    试试这个,

       this.initi=function(){
         $timeout(function() {
           $rootScope.customerDetails.traderType = $scope.traderTypes[1];   
           $scope.$apply();
        })
        };
    

    【讨论】:

    • 得到这个错误 angular_angular.js:12545 错误:[$rootScope:inprog] $digest 已经在进行中
    • 关于将 !$scope 更改为 !$rootScope 错误消失了...但默认值未设置
    • 你能试试这个吗,$timeout(function(){ $rootScope.customerDetails.traderType = $scope.traderTypes[1]; $scope.$apply(); });
    • 经过上述处理后给出了这个错误 angular_angular.js?hash=744f2a2…:12545 TypeError: v2.initi is not a function
    • 这是我给出的函数。有什么问题吗 this.initi= $timeout( function() { $rootScope.customerDetails.traderType = $scope.traderTypes[1]; $scope. $apply(); } );
    【解决方案2】:

    试试这个html

      <div class="form-group" ng-init="init()">
      <div class="col-lg-4">
      <select name="trader" class="form-control" id="select" ng-model="cus_traderType" ng-options="traderTypeObj.description for traderTypeObj in traderTypes 
     track by traderTypeObj.type" >
           </select>
            </div>
    

    app.js 代码

    请设置 $scope.cus_traderType 您的主控制器
    像这样

    $scope.cus_traderType = '';
    $scope.init=function(){
    
     $scope.cus_traderType = $scope.traderTypes[1];
    
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多