angular.js - angular 如何用指令给给列表动态进行class切换
本文介绍了angular.js - angular 如何用指令给给列表动态进行class切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
代码
想要的效果是,给当前点击的那个添加样式,其他的清空。
解决方案
不要这么麻烦了,咱们直接用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>
这篇关于angular.js - angular 如何用指令给给列表动态进行class切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文