AngularJs-ng-options在ajax调用后没有绑定

rep*_*rat 7 javascript ajax jquery angularjs ng-options

我尝试在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...
Run Code Online (Sandbox Code Playgroud)

这是My API发送的数据.

{ 
   "the_formula":{
     "123":"formula1",
     "124":"formula2"
   }
}
Run Code Online (Sandbox Code Playgroud)

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

Man*_*pta 11

@reptildarat

你好,

当我使用select和ng-option时,我也遇到了同样的情况.:)

你需要做什么 -

在从ajax调用中检索到的数据完成后,设置"foo.formula"的值.原因 - 正在呈现Html时.它绑定select标签中的"foo.formula".那时没有填充的项目.一段时间后,项目从后面(js)填充,并且没有触发该ng模型的触发器.

经过很多努力,我发现了这个 -

例-

这是你在js中需要做的.

   .success(function (data) {  
            $scope.$emit('HideLoading');  
            $scope.departments = data.DepartmentsGetResult;  
            $scope.selectedDepartment = item.DepartmentId;  
Run Code Online (Sandbox Code Playgroud)

这是我的HTML-

<select   
   data-ng-model="selectedDepartment"   
   data-ng-options="dept.DepartmentId as dept.Title for dept in departments" >  
 </select>  
Run Code Online (Sandbox Code Playgroud)

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