7

我尝试在 ajax 调用后更改 ng-options 的选定索引,但它不会改变。

//Html Section...

<select id="fooId" ng-model ="foo.formula"
   ng-options="formulas as name for (formulas, name) in the_formula"></select>

//End Html Section...


//js file...

//get list of formula from server...
TheSource.Get.then(function(response){
    $scope.the_formula = response.the_formula;
});


//do something awesome, then..
//binding data from server...

TheData.Get.then(function(response){
    //binding the data to view...
    //all of the element is binding, except the ng-options part..
    $scope.foo = response; 

    //not working..
    //$scope.formula = response.formulaId //it is return integer ID like (1, 2, 3, etc..)
});

// End js file...

这是我的 API 发送的数据。

{ 
   "the_formula":{
     "123":"formula1",
     "124":"formula2"
   }
}

怎么了?如何自动更改 ng-options 中的选择?

4

2 回答 2

11

@reptildarat

你好,

当我使用 select 和 ng-option 时,我也陷入了同样的境地。:)

你必须做什么——

在从 ajax 调用中检索到数据并完成绑定后,设置“foo.formula”的值。原因 - 呈现 Html 时。它在选择标签中绑定“foo.formula”。那时没有填充任何项目。一段时间后,项目从后面(js)填充,并且没有为该 ng-model 触发触发器。

经过一番努力,我发现了这个-

例子-

这是您需要在 js 中执行的操作。

   .success(function (data) {  
            $scope.$emit('HideLoading');  
            $scope.departments = data.DepartmentsGetResult;  
            $scope.selectedDepartment = item.DepartmentId;  

这是我的 HTML-

<select   
   data-ng-model="selectedDepartment"   
   data-ng-options="dept.DepartmentId as dept.Title for dept in departments" >  
 </select>  

希望这会有所帮助。
让我知道你的担忧。
:)

于 2014-03-12T11:41:03.770 回答
0

在 http 调用之后,使$scope.selectedOption = options[0]

当您使用ng-change.

<select ng-model="selectedOption" class="form-control" ng-change="metaTypeSelected(selectedOption)">
    <option 
        ng-selected="{{selectedOption}}" 
        ng-repeat="selectedOption in options track by $index"
        value="{{selectedOption}}">
        {{selectedOption}}
    </option>
</select>

在中使用selectedOptionng-modelng-repeatng-change

于 2021-11-10T07:12:03.467 回答