在AngularJS中触发Click事件时添加ng-class

时间:2015-11-20 08:26:03

标签: javascript css angularjs ng-class

我创建简单的应用程序。我添加评级功能。 我想在从控制器触发点击事件时更改评级星级。

在我的Controller中,我检查对象的评级==是否从视图中传递了数字,我返回班级名称active

在那之后,我仍然困惑如何传递我的回报值。

这是我的查看

<h3>{{ fruit.title }}</h3>
                <div class="rating">
                    <ul>
                        <li ng-click="addRating(1,fruit)"><i class="glyphicon glyphicon-star-empty"></i></li>
                        <li ng-click="addRating(2,fruit)"><i class="glyphicon glyphicon-star-empty"></i></li>
                        <li ng-click="addRating(3,fruit)"><i class="glyphicon glyphicon-star-empty"></i></li>
                        <li ng-click="addRating(4,fruit)"><i class="glyphicon glyphicon-star-empty"></i></li>
                        <li ng-click="addRating(5,fruit)"><i class="glyphicon glyphicon-star-empty"></i></li>
                    </ul>
                </div>

这是我的控制器

var app = angular.module('totalBuahApp', []);

app.controller('fruitController', function($scope) {
    $scope.fruits = [
        {
            title : 'Fresh Red Apple',
            img : 'https://cdn0.iconfinder.com/data/icons/fruits/512/Apple.png',
            description : 'Good for your daily consumption.',
            price : 15000,
            cartQty : 1,
            rating : 0
        },
        {
            title : 'Red Grape',
            img : 'http://www.freepngimg.com/thumb/grape/4-grape-png-image-download-picture-thumb.png',
            description : 'Like a king.',
            price : 34000,
            cartQty : 1,
            rating : 0
        }
    ];

    $scope.total = $scope.fruits.length;

    $scope.addRating = function(number, fruit) {
        fruit.rating = number;
    };

    $scope.ratingClass = function(number) {
        return (number == $scope.rating) ? "active" : "";   
    };
});

您可以查看我的完整代码 https://codepen.io/fanjavaid/pen/MaRWgG

2 个答案:

答案 0 :(得分:4)

不要为每个星形重复HTML,使用ngRepeat和ngClass:

<div class="rating">
    <ul>
        <li class="glyphicon glyphicon-star-empty"
            ng-click="addRating($index + 1, fruit)"
            ng-repeat="star in [0, 1, 2, 3, 4]"
            ng-class="{active: fruit.rating >= $index + 1}"></li>
    </ul>
</div>

演示: https://codepen.io/anon/pen/RWOPad?editors=101

答案 1 :(得分:1)

将您的评分Div替换为以下div

    <div class="rating">
       <ul>
           <li ng-click="addRating(1,fruit)"><i class="glyphicon glyphicon-star-empty" ng-class="{active:fruit.rating >= 1}"></i></li>
           <li ng-click="addRating(2,fruit)"><i class="glyphicon glyphicon-star-empty" ng-class="{active:fruit.rating >= 2}"></i></li>
           <li ng-click="addRating(3,fruit)"><i class="glyphicon glyphicon-star-empty" ng-class="{active:fruit.rating >= 3}"></i></li>
           <li ng-click="addRating(4,fruit)"><i class="glyphicon glyphicon-star-empty" ng-class="{active:fruit.rating >= 4}"></i></li>
           <li ng-click="addRating(5,fruit)"><i class="glyphicon glyphicon-star-empty" ng-class="{active:fruit.rating >= 5}"></i></li>
     </ul>

并在你的css中添加.active类。