javascript - Calculating how many times 'ng-repeat' has looped, then export as a string/value in Angular.js? -
i trying build 'like' function in angular.js app. have basic 'like' section, following code:
$scope.likeclicked = function () { if (!hasliked) { hasliked = true; $scope.liked = 'unlike'; $scope.likecount += 1; } else { hasliked = false; $scope.liked = 'like'; $scope.likecount -= 1; } }; what want achieve, , because 'likes provided json file loops 'ng-repeat', function calculates how many likes there in json file. note, json file being provided doesn't explicitly state 'likes' numerically, know them having 'name' object in file.
please help, have never used json before!
thanks.
jp
don't try answer out of ng-repeat. that's sorta backwards - why dom lookup piece of information that's available on scope? want this:
$http.get('likes.json').success(function(data) { $scope.likes = data; }); $scope.addlike = function(item) { // add item $scope.likes } then in html can like:
<p>you have liked {{likes.length}} items.</p> <ul> <li ng-repeat="item in items"> {{item}} <button ng-click="addlike(item)"></button> </li> </ul>
Comments
Post a Comment