[英]<select> AngularJS and ng-selected
我有这样的HTML代码:
<div ng-controller="SimpleController">
<select ng-model="item.category" ng-options="category as category.name for category in categories">
</select>
<br />Item: {{item | json}}
</div>
和js:
var app = angular.module("partnerModule", []);
app.controller("SimpleController", function($scope) {
$scope.item = {category: {name:'Cat1', id: '1'}, name : "Item name"};
$scope.categories = [{name:'Cat1', id: '1'}, {name: 'Cat2', id: '2'}, {name:'Cat3', id: '3'}];
});
我想在显示表单时已经为“ Cat1”设置选项。 我试图添加如下内容:
ng-selected="item.category.id == category.id"
对于<select>
和<option>
标记均无效
这将起作用:html
<div ng-controller="SimpleController">
<select ng-model="item.category" ng-options="category as category.name for category in categories track by category.id">
</select>
<br />Item: {{item | json}}
</div>
在JS中
var app = angular.module("partnerModule", []);
app.controller("SimpleController", function($scope) {
$scope.item = {category: {name:'Cat1', id: '1'}, name : "Item name"};
$scope.categories = [{name:'Cat1', id: '1'}, {name: 'Cat2', id: '2'}, {name:'Cat3', id: '3'}];
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.