简体   繁体   English

角度选择ng选择不起作用 <option ng-repeat> )

[英]angular select ng-selected not working (with <option ng-repeat>)

I've spent an hour and tried every conceivable permutation of properties to bind a select to a model as object, and ng-selected does nothing. 我花了一个小时,尝试了所有可能的属性排列,以将选择绑定到对象作为模型,而ng-selected则不执行任何操作。

<select ng-model="localModel.priceType">
    <option 
            ng-repeat="item in vm.priceTypes"
            ng-selected="localModel.priceType == item"
            ng-value="item"
            >{{item.name}}</option>
</select>

or 要么

<select ng-model="localModel.priceType">
    <option 
            ng-repeat="item in vm.priceTypes"
            ng-selected="localModel.priceType.id == item.id"
            ng-value="item"
            >{{item.name}}</option>
</select>

or 要么

<select ng-model="localModel.priceType">
    <option 
            ng-repeat="item in vm.priceTypes track by item.name"
            ng-selected="localModel.priceType.name == item.name"
            ng-value="item"
            >{{item.name}}</option>
</select>

The select list renders correctly, option values look funky ie "object:875". 选择列表正确呈现,选项值看起来很时髦,即“ object:875”。 and selected does not work. 并且选择不起作用。

I need ng-model to be the object, not object.someId. 我需要ng-model作为对象,而不是object.someId。

solved this by using ng-options instead of <option> ng-repat 通过使用ng-options代替<option> ng-repat解决了这个问题

<select ng-model="localModel.priceType" ng-options="item as item.namefor item in vm.priceTypes track by item.name"></select>

ngValue expects a string for the ngValue. ngValue需要ngValue的字符串。 To use ngRepeat with the <option> tag, then use value="{{item}}" instead of ng-value. 要将ngRepeat与<option>标记一起使用,请使用value="{{item}}"代替ng-value。 Expand the snippet below to see it working. 展开下面的代码片段以查看其是否正常运行。

 angular.module('myApp', []) .controller('ctrl', function($scope) { $scope.vm = { priceTypes: [{ id: 3, name: 'pound' }, { id: 5, name: 'Yen' }, { id: 6, name: 'dollar' } ] }; //select model value $scope.localModel = { priceType: $scope.vm.priceTypes[1] }; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> <div ng-app="myApp" ng-controller="ctrl"> <select ng-model="localModel.priceType"> <option ng-repeat="item in vm.priceTypes as item" ng-selected="localModel.priceType.id == item.id" value="{{item}}" >{{item.name}}</option> </select> <div> priceType: {{ localModel.priceType }} </div> </div> 

A simpler approach is to use ngOptions on the <select> tag, with a combination of forms: 一种更简单的方法是在<select>标记上使用ngOptions ,并结合以下形式:

select as label for value in array track by expr

Refer to the comprehension_expression forms in the Arguments section under Usage for more information. 有关更多信息,请参考“ 用法 ”下“ 参数”部分中的comprehension_expression表单。

 angular.module('myApp', []) .controller('ctrl', function($scope) { $scope.vm = { priceTypes: [{ id: 3, name: 'pound' }, { id: 5, name: 'Yen' }, { id: 6, name: 'dollar' } ] }; //select model value $scope.localModel = { priceType: $scope.vm.priceTypes[1] }; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> <div ng-app="myApp" ng-controller="ctrl"> <select ng-model="localModel.priceType" ng-options="item as item.name for item in vm.priceTypes track by item.name"> </select> <div> priceType: {{ localModel.priceType }} </div> </div> 

https://docs.angularjs.org/api/ng/directive/ngSelected https://docs.angularjs.org/api/ng/directive/ng已选择

ngSelected does not interact with the select and ngModel directives, it only sets the selected attribute on the element. ngSelected不会与selectngModel指令交互,它仅在元素上设置selected属性。 If you are using ngModel on the select, you should not use ngSelected on the options, as ngModel will set the select value and selected options. 如果在选择上使用ngModel ,则不应在选项上使用ngSelected ,因为ngModel将设置选择值和所选选项。

Try 尝试

<select ng-model="localModel.priceType">
  <option  ng-repeat="item in vm.priceTypes track by item.name"
           ng-value="item.name">
      {{item.name}}
  </option>
</select>

You can change ng-value to any value you want from vm.priceTypes[0] . 您可以从vm.priceTypes[0]ng-value更改为所需的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM