ng-options使用AngularJS V1.5隐藏了空白值 [英] ng-options is hiding the blank value with AngularJS V1.5

查看:81
本文介绍了ng-options使用AngularJS V1.5隐藏了空白值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照V1.3文档中指定的说明进行操作在我的选择之一中具有默认选项.

I followed the instructions specified in the V1.3 documentation to have a default option in one of my selects.

因此在我的Angular中̶1̶.̶3̶.̶1̶5̶我有1.5.7模板:

Therefore in my Angular ̶1̶.̶3̶.̶1̶5̶   1.5.7 template I have:

  <div class="form-group">
    <div class="col-md-12">
      <select ng-model="selectedObj" class="form-control"
              ng-options="obj.id as obj.name for obj in objects">
        <option value="">All objects</option>
      </select>
    </div>
  </div>

,然后在我的控制器中用以下命令对其进行初始化:

and in my controller I initialise it with:

$scope.selectedObj = '';
$scope.objects = [
  {
    id: '1',
    name: 'A',
  },
  {
    id: '2',
    name: 'B',
  },
  {
    id: '3',
    name: 'C',
  },
];

发生的是,没有显示所有对象"选项,而是显示了默认的空白选项",并且只有在第一次时才可以选择.空白选项可能是此处所述的一个.

What happens is that the "All objects" option is not shown and instead the default "blank option" is shown and is only selectable the first time. The blank option is probably the one described here.

如何使我的默认选项代替默认选项?我想念什么?

How can I make my default option work in place of the default one? What am I missing?

我刚刚发现bower_components/angular/.bower.json中的Angular版本被指定为1.5.7,而不是主bower.json文件指定的内容(即1.3.15).

I've just found out the Angular version inside bower_components/angular/.bower.json is specified as 1.5.7 as opposed to what the main bower.json file specifies (which is 1.3.15).

这可能是看似奇怪的行为的原因吗?

May this be the cause for the seemingly weird behaviour?

推荐答案

将ng模型设置为null:

 ̶$̶s̶c̶o̶p̶e̶.̶s̶e̶l̶e̶c̶t̶e̶d̶O̶b̶j̶ ̶=̶ ̶'̶'̶;̶
 $scope.selectedObj = null;

ng-option指令在V1.4中得到了严重的重构,在V1.5中已消除了许多错误.有关更多信息,请参见

The ng-option directive was heavily refactored in V1.4 and many bugs removed in V1.5. For more information, see

  • AngularJS Developer Guide - Migrating to V1.4 - ng-options
  • AngularJS Developer Guide - Migrating to V1.5 - directives ng-options

angular.module('app',[])
.controller("ctrl",function($scope) {
    $scope.selectedObj = null;
    $scope.objects = [
    {
        id: '1',
        name: 'A',
      },
      {
        id: '2',
        name: 'B',
      },
      {
        id: '3',
        name: 'C',
      },
    ];
})

<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
     <select ng-model="selectedObj" class="form-control"
             ng-options="obj.id as obj.name for obj in objects">
        <option value="">All objects</option>
     </select>
</body>

这篇关于ng-options使用AngularJS V1.5隐藏了空白值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆