oninput not working in Angular. Alternative?
Asked Answered
A

1

5

Im trying to create a function that reads the value of an input and triggers a series of true/false, however the code below keeps returning "passStrength is not defined." From what I can find, oninput isn't supported by Angular. How can achieve this in Angular?

Within my controller:

$scope.passStrength = function(input) {
    if (input.value.toLowerCase().indexOf(/[a-z]/) > -1) {
        $scope.lwrChar = true;
        console.log('lower ' + $scope.lwrChar);
    } else if (input.value.toUpperCase().indexOf(/[A-Z]/) > -1) {
        $scope.uprChar = true;
        console.log('upper ' + $scope.uprChar);
    } else if (input.value.indexOf() == !isNaN(n)) {
        $scope.nbrChar = true;
        console.log('number ' + $scope.nbrChar);
    } else if (input.value.length >= 8) {
        $scope.countChar = true;
        console.log('count ' + $scope.countChar);
    }
};

and in my markup:

<input id="password" oninput="passStrength()" />
Aldric answered 8/4, 2016 at 20:15 Comment(1)
The answer is in your tags: ng-change. docs.angularjs.org/api/ng/directive/ngChange.Huddle
D
7

To fire an event when the input changes, use ng-change. Also, you must define a ng-model.

<input ng-model="password" ng-change="passStrength(password)" />

Edit: Created a plunker demonstrating it

Denver answered 8/4, 2016 at 20:41 Comment(6)
Thanks but then how do I refer to the model in the controller? replacing input with $scope.password doesnt do the trick. Im getting an undefined error from the function for input.Aldric
Take a look at this Plunker.Denver
Thanks! I took a slightly different path but essentially the same!Aldric
Maybe it's worth mentioning that ng-change won't evaluate the expression whenever the model changes, but whenever it's changed by the user using the input field.Electrostriction
onchange and oninput are not the same, because onchange only fires when the field loses focus. While this answer may solve the OP's problem, it doesn't actually answer his question.Cheliform
I was talking about ng-change, not onchange, and it answers his question well, as in title oninput not working in Angular. Alternative?Denver

© 2022 - 2024 — McMap. All rights reserved.