【问题标题】:Value in Select using ng-options is not populating未填充使用 ng-options 选择的值
【发布时间】:2019-07-24 12:00:54
【问题描述】:

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

<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)

【问题讨论】:

  • $scope.mtList的内容是什么?
  • 我建议您在问题中添加新信息作为 cmets 或编辑。不是新的答案。
  • $scope.mtList的描述有问题。 __proto__ 应该显示 Array(6)&lt;&lt;FullUserDetails&gt;&gt; 缺少逗号。这些值是不可信的。
  • 你能创建一个PLNKR 来重现问题吗?
  • 它适用于 ... ng-options="mt as mt.description for mt in mtList track by mt.id"

标签: angularjs select ng-options


【解决方案1】:

使用选择表达式的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

【讨论】:

  • 谢谢,我试过了,但是不行,进入编辑页面还是空白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
相关资源
最近更新 更多