angular.js - angularjs如何实现点击li添加class,再次点击removeclass,且不相互影响

浏览:31日期:2023-02-02

问题描述

<!DOCTYPE html><html ng-app='myapp'><head lang='en'> <meta charset='UTF-8'> <title></title> <script src='https://www.6hehe.com/wenda/angular.min.js'></script> <script> var m1 = angular.module(’myapp’,[]);m1.controller(’Aaa’,function($scope){ $scope.colors = [{’id’:’1’,’name’:’red’ },{’id’:’2’,’name’:’green’ },{’id’:’3’,’name’:’blue’ }];}); </script></head><body> <p ng-controller='Aaa'><ul> <li ng-repeat='color in colors'>{{color.name}}</li></ul> </p></body></html>

需要用到id对应起来吗?

问题解答

回答1:

不用楼上那么复杂。

<ul> <li ng- ng-click='color.active = !color.active' ng-repeat='color in Texture'>{{color.value}}</li></ul>回答2:

题主应该使用angular的directive来实现这个需求.结果: http://output.jsbin.com/rolobakaya

javascriptm1.directive(’toggleClass’, function(){ return {restrict: ’A’,scope: { toggleClass: ’@’},link: function($scope, $element){ $element.on(’click’, function(){$element.toggleClass($scope.toggleClass); });} };});

html<li toggle-class='classname'></li>回答3:

我的思路是:

<li ng- ng-repeat='color in colors'>{{color.name}}</li>

然后定义一个ng-click并在里面修改$scope.currentId

相关文章: