Kim Sean Kim Sean - 3 months ago 17
AngularJS Question

angularJs ng-click is not working inside the ng-if function

<div ng-if="quantity > 0">
<button ng-click="quantity = quantity-1">-</button>
</div>


Above is my code. i dont know why the ng-click doesnt work when it shows up. anyone can give me advise? thanks.

Answer

Try this it will work :

<div ng-controller="MyCtrl">
  <div ng-show="quantity > 0">
    <button ng-click="quantity = quantity-1">-</button>
    <p>{{quantity}}</p>
  </div>
</div>

app.controller('MyCtrl', function($scope) {
  $scope.quantity = 6;
});

Working fiddle : http://jsfiddle.net/Lvc0u55v/8856/