1

When user type in number then click on "+", it will append the count instead of perform addition for the counter.

But it is working fine when performing "-" or "+" action without key in number in the input box.

<div ng-app="myApp" ng-controller ="myCtrl">
  <span ng-click="count = count + 1">+</span>
  <input type="text" ng-model="count" valid-number>
  <span ng-click="count = count - 1"  ng-show="count > 0">-</span>
</div>

fiddle : http://jsfiddle.net/shenglim/Lyugypqk/4/

Anybody can help out on this? Thanks !

0

3 Answers 3

1

simple example will work if uses ng-click="test()" and perform basic addition, if you need to retrieve from existing json which return string, Try to use parseInt() on the JSON object.

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

Comments

1

Try <input type="number">.

The data type for type="text" makes it a string and "1" + 1 = "11"

JSFiddle ~ http://jsfiddle.net/Lyugypqk/2/

3 Comments

Hi Phil, The reason i put input type ="text" is to prevent user from input negative value by directive control. If type="number" user still able to input negative by click arrow down.
@Took then add min="0".
Sorry that I didn't stated clearly in the question asked. I wanted to prevent negative and decimals from user.
1

Alternatively you can add two methods to increase and decrease the values into the controller and use them,

    $scope.increment = function(){
        $scope.count++;
    };

    $scope.decrement = function(){
        $scope.count--;
    };

    <span ng-click="increment()">+</span>
    <input type="text" ng-model="count" valid-number>
    <span ng-click="decrement()" ng-show="count > 0">-</span>

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.