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. javascript - H5下拉能更换页面么 怎么实现 在左右滑的页面中2. 记不住代码有什么好的方法吗?3. vue计算属性怎么样与for结合使用4. angular.js - angularJS最简单方法实现点击checkbox,button变亮5. angular.js - 前端使用input使用ng-model绑定数据,控制前中一直无法得到映射的值6. java - 当系统出现线程死锁时怎么处理?7. angular.js - 关于指令link 中的创建变量问题8. MySQL数据库服务器循环插入执行速度慢9. 电脑上装了wamp后,再装了mysql,wamp上的mysql就不能用了10. javascript - 请问微信jssdk自定义分享是不是不能用了?