Anuj Gupta Anuj Gupta - 22 days ago 6
AngularJS Question

Dynamic Update value on click counter Angular Js

I created a Post page where i want to update value on click on like or dislike .I created a fiddle for this

Fiddle:- [https://jsfiddle.net/anujsphinx/k017gsz8/][1]


.When i click on like then Like counter will increase and when i click on dislike then dislike counter will increase.

Answer

You need to have two function to add and remove,

HTML:

 <div class="post" ng-repeat="post in posts">
      <h3>{{post.title}}</h3>
      <a href="#" ng-click="add(post)">like</a> ({{post.like}})
      <a href="#" ng-click="remove(post)">Dislike</a> ({{post.dislike}})
    </div>

Controller:

 $scope.add = function(post) {
    post.like +=1;

  }
  $scope.remove = function(post) {
    post.dislike +=1;
  }

DEMO