在select标记中显示为空的第一个选项

时间:2015-03-28 10:59:39

标签: angularjs

我的应用程序显示一个空的选择字段作为第一个条目。我已经阅读了给出的其他解决方案并尝试了它但仍然无效。

我的控制器代码是:

.controller('userCreateController', function(User,Profile,Auth) {
      alert("hello");
      var vm = this;
      vm.type = 'create';
     Profile.all() //this service fetch all profiles to be shown in select field
     .success(function(data){
      vm.profile = data;

});
vm.userData.profile = vm.profile[0]; //here i have set default value for select field

我的html页面是:

<label class="col-sm-2 control-label">Profile</label>
<div class="col-sm-8">
  <select ng-model="user.userData.profile" ng-options="person._id  as person.profileName for person in user.profile">
  </select>
</div>

vm.profile的层次结构

{
  "_id": 46,
  "profile": ObjectId("5516498e95b84548156db754"),
  "isActive": true,
  "password": "$2a$10$h8WTqsbNzTA5EqUcPUllYOCSjt0YDCBULEvDcntkg/muEHpAwX/xO",
  "username": "admin",
  "name": "admin",
  "reciptBook": [],
  "__v": 0
}, {
  "_id": 48,
  "profile": ObjectId("55168b8fbf769e6407ae3603"),
  "isActive": false,
  "password": "$2a$10$DnE84kyL9LI8tE3Fxet5su2ysjzRwnDXeriWOui3iDAki6eb53qn.",
  "username": "hello",
  "name": "hello",
  "reciptBook": [],
  "__v": 0
}

2 个答案:

答案 0 :(得分:1)

尝试添加空选项,该选项将具有未定义的值。

<select ng-model="user.userData.profile" ng-options="person._id  as person.profileName for person in user.profile">
  <option value=""></option>
</select>

答案 1 :(得分:0)

使用vm.userData.profile = vm.profile[0]._id;因为您在模型中设置了person._id,然后选择。

<强>更新: -

我为您添加了最低限度的所需羽毛球,请检查。 Plunker

相关问题