<select> AngularJS和ng-selected

时间:2016-06-01 15:26:50

标签: javascript html angularjs

我有这样的HTML代码:  &lt; div ng-controller =&#34; SimpleController&#34;&gt;     &lt; select ng-model =&#34; item.category&#34; ng-options =&#34; category as category.name in category in category&#34;&gt;     &LT; /选择&GT;     &lt; br /&gt;项目:{{item | JSON}}  &LT; / DIV&GT; 和js: var app = angular.module(&#34; partnerModule&#34;,[]); app.controller(&#34; SimpleController&#34;,function($ scope){     $ scope.item = {category:{name:&#39; Cat1&#39;,id:&#39; 1&#39;},name:&#34; Item name&#34;};     $ scope.categories = [{name:&#39; Cat1&#39;,id:&#39; 1&#39;},{name:&#39; Cat2&#39;,id:&#39; 2&# 39;},{name:&#39; Cat3&#39;,id:&#39; 3&#39;}]; }); 我想已经为&#34; Cat1&#34;设置了选项。何时显示表单。我试着添加这样的东西: ng-selected =&#34; item.category.id == category.id&#34; 对于两者&lt; select&gt;和&lt;选项&gt;标签它没有工作

1 个答案:

答案 0 :(得分:1)

这将有效: 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'}];
});