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

时间:2014-03-12 10:40:08

标签: javascript jquery ajax 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...

这是My API发送的数据。

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

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

1 个答案:

答案 0 :(得分:11)

@reptildarat

您好,

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

你需要做什么 -

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

经过大量的努力,我找到了这个 -

示例 -

这是你需要在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>  

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