http://plnkr.co/edit/nEfBE33AJen3mz9YvjeC?p=preview
我有一个标签按钮列表,默认情况下,加载所有项目后,我将前 3 个项目设置为具有selected
该类。
现在我还需要标签来获得或失去selected
.ng-click
您将如何组合标记或控制器中的代码?
.controller('PageCtrl',
['$scope',
function($scope) {
var vs = $scope;
vs.message = "1st, 2nd and 3rd item should be selected by default:";
// Gives the 1st 3 items the selected class
vs.toggleTags = { item: [0, 1, 2] };
// the ng-click to individually toggle the selected class:
vs.selectTag = function(term) {
alert(term +' in tag #'+ vs.toggleTags.item);
};
vs.tags = [
{name: 'aaa'},
{name: 'bbb'},
{name: 'ccc'},
{name: 'ddd'},
{name: 'eee'},
{name: 'fff'},
{name: 'ggg'}
];
}]);
HTML
<ul>
<li ng-repeat="(k, m) in tags"
ng-class="{'selected':toggleTags.item.indexOf(k) > -1}"
ng-click="toggleTags.item = $index; selectTag(m.name)">
<div class="tag">{{m.name}}</div>
</li>
</ul>
基本上我想结合类似{'selected' : toggle.state}
的东西: 成行ng-class
。现在我的代码取消选择所有内容。
有任何想法吗?