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. html - 移动端radio无法选中2. apache - 怎么给localhost后面默认加上8080端口3. css - 关于伪类背景问题4. python - 管道符和ssh传文件5. c++ - win7在不删除管理员密码的前提下(密码不为空),如何设置开机不需要密码?6. mysql - 数据库JOIN查询7. windows-7 - Win7中Vmware Workstatoin与Xampp中Apache服务器端口冲突?8. 关于Navicat连接到mysql,我改了root的密码后,Navicat连接报错1862?9. php7.3.4中怎么开启pdo驱动10. python - 用scrapy-splash爬取网站 为啥iframe下的内容没有被返回
