2
<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.

8
  • what is your initial quantity value ? Commented Aug 29, 2016 at 11:35
  • Post your complete html and controller code as well. Commented Aug 29, 2016 at 11:36
  • please provide your ngController code where you assign $scope.quantity. Commented Aug 29, 2016 at 11:37
  • see the plunker its working correctly. Commented Aug 29, 2016 at 11:39
  • check the answer with the working fiddle.I hope it is as per expectation. Commented Aug 29, 2016 at 11:45

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

Comments

1

Angular has its own "Dot" rule.

If you keep the quantity stored in controller scope, you might try this inside controller:

$scope.myvar = {
    quantity: 2,
    decrease: function() {
        this.quantity--;
    }
}

and html

<div ng-if="myvar.quantity > 0">
 <button ng-click="myvar.decrease()">-</button>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.