0

我对我正在做的事情做了一点小小的改动,虽然它不会奇怪地在小提琴中工作,但会在我的版本中逐渐添加一个类似的东西:http: //jsfiddle.net/LQFrv/

function($scope) {

var hasLiked = false;

$scope.likeClicked = function() {

    if( hasLiked === false ){
        $scope.likeCount = $scope.likeCount + 1;
    }

    hasLiked = true;

    if (hasLiked === true) {
        $scope.liked = 'Unlike';
    }

};

});

我想要做的是:添加一个'like'>禁用'like'并替换为'unlike'>在下次单击元素时,删除like

请帮忙!

谢谢,

J.P

4

1 回答 1

4

HTML:

<body ng-app ng-controller="Ctrl"> 
    <a ng-click="likeClick()" ng-init="liked='Like'; likeCount=0">
        {{liked}} {{likeCount}}
    </a>
</body>

JS:

function Ctrl($scope) {
var hasLiked = false;
$scope.likeClick = function () {
    if (!hasLiked) {
        hasLiked = true;
        $scope.liked = 'Unlike';
        $scope.likeCount += 1;
    } else {
        hasLiked = false;
        $scope.liked = 'Like';
        $scope.likeCount -= 1;
    }
};

}

工作小提琴:jsfiddle.net/LQFrv/4/
希望有帮助!

编辑:搞砸了链接,它导致另一个小提琴,对不起,现在应该是正确的!

于 2013-04-09T13:22:53.837 回答