admin管理员组

文章数量:1424409

I have made a little fiddle of what I am doing, although it won't work in fiddle oddly, but does incrementally add a like in my version: /

function($scope) {

var hasLiked = false;

$scope.likeClicked = function() {

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

    hasLiked = true;

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

};

});

What I am trying to do is: Add a 'like' > disable 'like' and replace with 'unlike' > on next click of element, remove the like

Please help!

Thanks,

JP

I have made a little fiddle of what I am doing, although it won't work in fiddle oddly, but does incrementally add a like in my version: http://jsfiddle/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';
    }

};

});

What I am trying to do is: Add a 'like' > disable 'like' and replace with 'unlike' > on next click of element, remove the like

Please help!

Thanks,

JP

Share Improve this question asked Apr 9, 2013 at 12:44 JohnRobertPettJohnRobertPett 1,1834 gold badges22 silver badges37 bronze badges 1
  • use a browser console to look at errors. Fiddle immediately throws error SyntaxError: function statement requires a name and you have no ng-controller in fiddle html. What is the question? – charlietfl Commented Apr 9, 2013 at 12:59
Add a ment  | 

1 Answer 1

Reset to default 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;
    }
};

}

Working fiddle: jsfiddle/LQFrv/4/
Hope that helps!

edit: messed up with the link, it lead to another fiddle, sorry, now it should be correct!

本文标签: javascriptAdding a 39like39 button in AngularjsStack Overflow