angular.js - angular 如何用指令给给列表动态进行class切换
问题描述
代码
想要的效果是,给当前点击的那个添加样式,其他的清空。
问题解答
回答1:不要这么麻烦了,咱们直接用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- 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>回答2:
用属性绑定的方式给 ulList 里的每个元素加个 isSelected 属性,当 Click 时更改 isSelected 的值HTML 中用 ng-class
相关文章:
1. node.js - nodejs中mysql子查询返回多行结果怎么处理?2. python3.x - python中import theano出错3. javascript - 我写的href跳转地址不是百度,为什么在有的机型上跳转到百度了,有的机型跳转正确4. 这段代码是获取百度收录量的!需要怎么设置才能获取百度快照旁边的网址呢?5. 设置python环境变量时不小心把原始path都删了,请问怎么恢复? win7 64位的6. python - mysql 如何设置通用型字段? 比如像mongodb那样7. mysql - spring data jpa 方法sql复杂查询?8. node.js - 微信的自动回复问题9. git - 使用淘宝npm安装hexo出现问题?10. python - pandas按照列A和列B分组,将列C求平均数,怎样才能生成一个列A,B,C的dataframe