angular.js - angularjs如何实现点击li添加class,再次点击removeclass,且不相互影响
世界只因有你
世界只因有你 2017-05-15 16:52:53
0
3
2522
<!DOCTYPE html>
<html ng-app="myapp">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="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对应起来吗?

世界只因有你
世界只因有你

reply all(3)
给我你的怀抱

It doesn’t have to be as complicated as the one above.

<ul>
    <li ng-class="{red : color.active}" ng-click="color.active = !color.active" ng-repeat="color in Texture">{{color.value}}</li>
</ul>
左手右手慢动作

The subject should use Angular’s ​​directive to achieve this requirement.
Result: 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>
小葫芦

My thoughts are:

<li ng-class="{active: currentId == color.id}" ng-repeat="color in colors">{{color.name}}</li>

Then define a ng-click并在里面修改$scope.currentId

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template