A
A
Alexander Wolf2013-08-28 22:27:10
JavaScript
Alexander Wolf, 2013-08-28 22:27:10

How to get $event in ngChange

Hello! I have an input

<input name='login' type='text' ng-change='change()' ng-model='login' />
And when I change it, the method twitches change().

Now the question is: how do I get exactly the input in which the text change occurred?

Reason: Change the input'a class if it doesn't meet the required requirements and return the old class if it does.

PS: If there are more reasonable methods, please tell me!
PPS: When using ngClick, you can pass the $event argument, from which you could pull the link itself.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
XimikS, 2013-08-28
@mannaro

If it’s visual
image
image
Sorry for a bunch of code - just generated simple_form =)
In general, it’s possible and easier to do this:

A
Anton, 2013-08-28
@sHinE

As far as I understand, it is better to do validation in directives, for example, like here: www.ng-newsletter.com/posts/validations.html in the Custom validations section

S
Sergey, 2013-08-28
Protko @Fesor

Input validation should be done only through directives dependent on ngModel. For example something like this:

angular.module('validators', [])

.directive('maxlength', function () {
    
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function ($scope, $el, attrs, ngModelCtrl) {
            ngModelCtrl.$parsers.unshift(validator);
            ngModelCtrl.$formatters.unshift(validator);
            
            function validator (value) {
                    if (value.length > attrs.maxlength) {
                           ngModelCtrl.$setValididy(false, 'maxlength')
                    }

                    return value;
            }
        }
    }
})


The example is not working but should explain the essence. Why is it beneficial? Reusability increases many times, you don’t need to fence your bikes, at the same time everything is tied to the scope and you can check the validity of the fields by referring to the $valid property of both the field itself and the entire form.
In general, everything related to any kind of work with the DOM should be placed in directives. and onchange and ngModel are only needed for the controller or other directives.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question