angularjs - angular edit each post in array -
i have array of posts:
.factory('posts', [function(){ var o = { posts: [ {title: 'post 1', upvotes: 5}, {title: 'post 2', upvotes: 2}, {title: 'post 3', upvotes: 15} ] }; return o; }])
and controller. edit upvotes when clicking button, have following.
angular.foreach($scope.posts, function(title){ $scope.posts.push({ title: "ja" }); });
but creating new posts. knows how edit existing ones? thanks
to update upvotes value of posts on clicking button have change foreach to:
angular.foreach($scope.posts, function(post){ post.upvotes++; });
but if specifc 1 called 'ja' need this:
angular.foreach($scope.posts, function(post){ if (post.title === 'ja') { post.upvotes++; } });
Comments
Post a Comment