加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS.当select具有ng-model属性时,ng-selected不起作用

发布时间:2020-12-17 17:44:12 所属栏目:安全 来源:网络整理
导读:我正在尝试在我的选项元素中设置ng-selected,选择的属性设置为true,但选项未选中,当我从select all中删除ng-model时变得有效. 问题:当我使用模型时,如何选择选项? 这是my plunker(选择不在这里工作) 我的代码: var app = angular.module("plunker",[]);ap
我正在尝试在我的选项元素中设置ng-selected,选择的属性设置为true,但选项未选中,当我从select all中删除ng-model时变得有效.

问题:当我使用模型时,如何选择选项?

这是my plunker(选择不在这里工作)

我的代码:

var app = angular.module("plunker",[]);

app.controller("TestController",["$scope",function($scope) {
  $scope.test = 1;
  $scope.array = [
        {"id": 1,"name": "first"},{"id": 2,"name": "second"},{"id": 3,"name": "third"}
      ];
}]);

我的模板:

<body ng-controller="TestController">
    Selected item must be {{ array[test-1].name }}
    <select ng-model="myModel">
      <option value="">Choose item..</option>
      <option ng-repeat="item in array" 
              ng-value="item.id" 
              ng-selected="item.id == test">
        {{ item.name }} ({{item.id == test}})
      </option>
    </select>
  </body>

非常感谢您的帮助!

解决方法

不要在ngRepeat中使用ngSelected.使用ngOptions:

<body ng-controller="TestController">
    Selected item must be {{ array[test-1].name }}
    <select ng-model="myModel" ng-options="item.id as item.name for item in array">
      <option value="">Choose item..</option>
    </select>
  </body>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读