0

I have been trying to set minimum value 1 for a input text box "number". And i have a button to decrement it (using angularjs). Even though i have set min="1" on clicking the button its goes below 1. How can i maintain the minimum value 1? Below is the code

https://jsfiddle.net/3uyoLqhg/2/

<body ng-app="">
<input ng-model="quan" ng-init="quan=5" min="1" type="number"/>
<button ng-click="quan=quan-1">minus</button>

2 Answers 2

4

The input min directive is for validation, not for restricting model value. Chrome does have these tiny up and down arrows appear inside the right edge of the input box that respects min attribute, but that's about it. You just have to write your own restriction logic like

<button ng-click="quan = quan > 1 ? quan - 1 : quan">minus</button>

Fiddle

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

Comments

0

If your min is going to be 1 then you can use short circuit evaluation

 <button ng-click="quan=(quan-1) || 1">minus</button>

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.