某草草2017-05-15 17:04:38
不要这么麻烦了,咱们直接用ng-class
吧,像这样:
修正:
好吧,我错了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="x in ulList" ng-class="{active: currentIndex === $index}" ng-click="handleActive($index)">{{ x.li }}</li>
</ul>
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function($scope){
$scope.handleActive = function(index){
$scope.currentIndex = index;
};
$scope.ulList = [
{
id: '1',
li: '01'
},{
id: '2',
li: '02'
},{
id: '3',
li: '03'
},{
id: '4',
li: '04'
}
];
}]);
</script>
</body>
</html>
伊谢尔伦2017-05-15 17:04:38
用属性绑定的方式
给 ulList 里的每个元素加个 isSelected 属性,当 Click 时更改 isSelected 的值
HTML 中用 ng-class