AngularJS.当选择了NG-模型属性,NG-选择不工作 [英] AngularJS. When select have ng-model attribute, ng-selected not working

查看:56
本文介绍了AngularJS.当选择了NG-模型属性,NG-选择不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的option元素中设置ng-selected,将selected属性设置为true,但是未选择option,当我从select中删除ng-model时,所有文件都可以使用.

I'm trying to set ng-selected in my option element, selected attribute is set to true, but option not selected, When I remove ng-model from select all become working.

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

The question: How to make option selected, when I'm using the model?

这是我的朋克车(selected在这里不起作用)

Here is my plunker (selected not working here)

我的代码:

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>

非常感谢您的帮助!

推荐答案

不要将ngSelected与ngRepeat一起使用.使用ngOptions:

Don't use ngSelected with ngRepeat. Go with 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>

这篇关于AngularJS.当选择了NG-模型属性,NG-选择不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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