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 nong-controller
in fiddle html. What is the question? – charlietfl Commented Apr 9, 2013 at 12:59
1 Answer
Reset to default 4HTML:
<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
版权声明:本文标题:javascript - Adding a 'like' button in Angular.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745423611a2658012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论