<!DOCTYPE html>
<html ng-app="myapp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="angular.min.js"></script>
<script>
var m1 = angular.module('myapp',[]);
m1.controller('Aaa',function($scope){
$scope.colors = [{
'id':'1',
'name':'red'
},{
'id':'2',
'name':'green'
},{
'id':'3',
'name':'blue'
}];
});
</script>
</head>
<body>
<p ng-controller="Aaa">
<ul>
<li ng-repeat="color in colors">{{color.name}}</li>
</ul>
</p>
</body>
</html>
需要用到id對應嗎?
给我你的怀抱2017-05-15 16:54:53
不用樓上那麼複雜。
<ul>
<li ng-class="{red : color.active}" ng-click="color.active = !color.active" ng-repeat="color in Texture">{{color.value}}</li>
</ul>
ringa_lee2017-05-15 16:54:53
題主應該使用angular的directive來實現這個需求.
結果: http://output.jsbin.com/rolobakaya
javascript
m1.directive('toggleClass', function(){ return { restrict: 'A', scope: { toggleClass: '@' }, link: function($scope, $element){ $element.on('click', function(){ $element.toggleClass($scope.toggleClass); }); } }; });
html
<li toggle-class="classname"></li>
高洛峰2017-05-15 16:54:53
我的思路是:
<li ng-class="{active: currentId == color.id}" ng-repeat="color in colors">{{color.name}}</li>
然後定義一個ng-click
并在里面修改$scope.currentId