未使用ng-options选择中的值填充

时间:2019-03-02 18:44:31

标签: angularjs select ng-options

这是一个基本查询,但由于某些原因而失败,需要帮助。

尝试编辑实体时,

<select>未填充值。它应该显示此属性从db返回的值,但不显示,我也有ng-model了。我相信它应该显示出捐助者的价值,但是它显示出空白。还尝试了ng-model = userDTO.memberType.description

我在哪里出错呢?

<select     
    name="memberType"
    class="form-control"
    ng-model="userDTO.memberType"
    ng-options="mt.description for mt in mtList">                           
</select>

控制器代码:

userService.findUserWithContactAndAddress($stateParams.user).then(
    function(response) {                        
        $scope.userDTO = response;      
        console.log("<<FullUserDetails>>:", response);
}); 

服务响应

<<FullUserDetails>>: 
  {id: 77, 
   donorType: {
     id: 2,
     desc: "Organization"
   } 
   memberType: {
     id: 4, 
     description: "Donor",
     $$hashKey: "object:45"
   }, 
   $$hashKey: "object:44"
 }

请澄清一下,我确实从db下拉列表中获得了完整的值列表,但是在editUser函数时特定用户的选定值并未填充,而是MemberType变为空白。希望它清除。


$ scope.mtList返回

MemberTypes : 
0: {id: 2, description: "CMA Member", $$hashKey: "object:96"}
1: {id: 4, description: "Donor", $$hashKey: "object:97"}
2: {id: 3, description: "Employee", $$hashKey: "object:98"}
3: {id: 6, description: "Other", $$hashKey: "object:99"}
4: {id: 1, description: "Trustee", $$hashKey: "object:100"}
5: {id: 5, description: "Volunteer", $$hashKey: "object:101"}
length: 6
__proto__: Array(0)

1 个答案:

答案 0 :(得分:0)

使用选择表达式的select as形式:

<select     
    name="memberType"
    class="form-control"
    ng-model="userDTO.memberType"
    ̶n̶g̶-̶o̶p̶t̶i̶o̶n̶s̶=̶"̶m̶t̶.̶d̶e̶s̶c̶r̶i̶p̶t̶i̶o̶n̶ ̶f̶o̶r̶ ̶m̶t̶ ̶i̶n̶ ̶m̶t̶L̶i̶s̶t̶"̶
    ng-options="mt as mt.description for mt in mtList">                           
</select>

有关更多信息,请参见AngularJS ng-options API Reference - select as