2016-04-22 30 views
1

我试图定义一个函数,可以用来设置选择的默认值。选择正确填充,但传递给函数的值未定义。我该如何做到这一点?传递数组到范围函数从ng-init?

这是我的HTML。

 <div id="Option" class="form-group" ng-show="Options.length > 0"> 
      <div class="col-sm-1 col-md-1 col-lg-2" /> 
      <label class="control-label col-sm-3 col-md-3 col-lg-2">Options</label> 
      <div class="col-sm-2 col-md-2 col-lg-2" > 
       <select name="OptionSelect" id="OptionSelect" class="form-control" 
         ng-model="OptionSelectedId" 
         ng-init="OptionSelectedId = getDefaultOption(Options)" 
         ng-options="Option.objectKey as Option.label for Option in Options" required > 
       </select> 
      </div> 
     </div> 

这是控制器范围的功能

$scope.getDefaultOption=function(arr){ 
     return arr=$.grep(arr, function (dropdownObj) { 
      return dropdownObj.default === true; 
     })[0]; 

} 

回答

2

试试这个:

ng-init="getDefaultOption(Options)" 

$scope.getDefaultOption=function(arr){ 
     arr=$.grep(arr, function (dropdownObj) { 
      $scope.OptionSelectedId = dropdownObj.default === true; 
     })[0]; 

} 
+0

在NG-初始参数只是CAL功能 - 你的函数应该设置$ scope.OptionSelectedId内js文件,而不是像您的示例中查看的那样。希望它有助于解决你的问题:) – 2016-04-22 14:11:12

相关问题