0

I need an input value to be limited to [1, 5000000]. I want to do it programmatically. I am using AngularJS for my web application. Here is the code I wrote:

html

<input ng-change="checkLimit()" type="number" min="1" max="5000000" name="input" ng-model="data.flips" required/>

js

angular.module("app", []).controller("controller", function($scope) {
    $scope.checkLimit = function() {
        $scope.data.flips = $scope.data.flips > 5000000 ? 5000000 : 
        $scope.data.flips;
        $scope.data.flips = $scope.data.flips < 1 ? 1 : $scope.data.flips;
    }
});

I know for sure that the function runs, but the value in the input field and the variable is not updated. I am relatively new to Angular, so there is probably some elementary concept that I am missing.

1
  • Have you debugged the script to see what the value of $scope.data.flips is? Commented Jul 13, 2017 at 0:17

2 Answers 2

1

You need to remove min and max attributes, due to this validation does not allow to change the ng-model.

Alternative if you want to maintain the min and max you can add ng-model-options like this:

<input ng-change="checkLimit()" type="number" min="1" max="5000000" name="input" ng-model="data.flips" required
                           ng-model-options="{ allowInvalid: true, updateOn: 'blur' }"/>

Note that we are using ng-model-options, thus we have to use the appropriate angular version >=1.4

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

1 Comment

JIC: It updates the data on blur event in the input. if you want it to update right away, remove the updateOn: 'blur' from the ng-model-options. But I guess it is user friendly with the onblur.
1

Edit: see cccross' answer for a cleaner version

You need to escape the current digest cycle, as I don't believe that angular will update an element with a new property value if the property value was updated in a change event from that element.

There may be cleaner ways to do what you request; however, this example works. I removed the 'min' and 'max' input attributes, and moved your ng-change listener logic into a '$timeout' call. $timeout is included as a controller argument. $timeout will trigger another digest cycle, which will update your input.

angular.module("app", []).controller("controller", function($scope, $timeout) {
    $scope.checkLimit = function() {
        $timeout(() => {
            $scope.data.flips = $scope.data.flips > 5000000 ? 5000000 : 
            $scope.data.flips;
            $scope.data.flips = $scope.data.flips < 1 ? 1 : $scope.data.flips;
        })
    }
});

fiddle

10 Comments

See my answer for a cleaner solution
I don't think your answer is correct - your answer fails to update the value displayed in the input element. jsfiddle.net/xaxwaudv/4
It works. It updates the data on blur event in the input. if you want it to update right away, remove the updateOn: 'blur' from the ng-model-options. But I guess it is user friendly with the onblur.
I could have created a bad example, but I don't see what I think is your answer working at this fiddle: jsfiddle.net/xaxwaudv/5
wrong fiddle link. here's the correct link: jsfiddle.net/xaxwaudv/5
|

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.