How to get the $event in ngChange

Hello! I have input
<input name="login" type="text" ng-change="change()" ng-model="login">
And if you change it I have a twitch method change().

Now the question is: how do I get that input in which there is a change of text?

Reason: to Change the input's class a if it does not meet the necessary requirements and return the old class if you satisfy.

PS: If there are more reasonable methods, please tell me!
P. P. S: When you use ngClick, you can pass the argument $event, from which it was possible to get the link.
October 3rd 19 at 04:11
3 answers
October 3rd 19 at 04:13
Solution
If clearly
image
image

Sorry for a bunch of code — so that simple_form were lots of =)

Generally it is possible and easier to do this:
using ng-class - naomi.Hamill72 commented on October 3rd 19 at 04:16
Not) Then I have to use a name field (which makes the script more cumbersome).
Ctrl = ($scope) ->
 $scope.change = (e) ->
 alert e.target #HTMLDOMElement
- nico.Hodkiewicz0 commented on October 3rd 19 at 04:19
mannaro, did not understand the last comment.
ng-class="{{ registerForm.login.$valid ? "class1" : "class2 }}"

it? - naomi.Hamill72 commented on October 3rd 19 at 04:22
See.
I did this:
<input name="login" type="text" ng-change="change()" ng-model="login" ng-class="{{ registerForm.login.$valid ? 'success' : 'error' }}">

And then wanted to change the name from input'a. Then I have to change and ng-class:) - nico.Hodkiewicz0 commented on October 3rd 19 at 04:25
And, by the way, angular swears on the question mark in the expression. And the description says:
You cannot write a control flow statement in an expression. The reason behind this is core to the Angular philosophy that application logic should be in controllers, not in the view. If you need a conditional, loop, or to throw from a view expression, delegate to a JavaScript method instead.
- nico.Hodkiewicz0 commented on October 3rd 19 at 04:28
Yes, the ternary operator in templates is not supported.
There should be either this:
<input name="login" type="text" ng-change="change()" ng-model="login" ng-class="{ 'success' :registerForm.login.$valid, 'error':registerForm.login.$invalid }">

Or (in General) for ng-style, for example, ng-style="{backgroundColor: (condition) && true_value || false_value}" - Raleigh_Hartmann commented on October 3rd 19 at 04:31
And, I even forgot, and wrote with two braces. Template compilers and successfully works lol :D
In any case, why is it all necessary?
Angular machine all sorts of ng-valid ng-invalid ng-invalid-required classes element will add them and you can dance to. - naomi.Hamill72 commented on October 3rd 19 at 04:34
But if I want for example to show a tooltip? How am I supposed to jump from ng-valid and ng-invalid?
In General, as I understand it, we'll still have to specify the name of the field. To do so:
Ctrl = ($scope) ->
 $scope.change = ->
 if @form.login.$valid
#Success
else
#Error
- nico.Hodkiewicz0 commented on October 3rd 19 at 04:37
Yes. And what is the problem to specify a field name, of anyways to change it then?

By the way, it is possible to make the tooltip ng-show="form.login.$valid" and then ng-animate to play.
And if you are using bootstrap, at all convenient — ready wrapper is already there - naomi.Hamill72 commented on October 3rd 19 at 04:40
October 3rd 19 at 04:15
I understand that validation should be done in directives, such as here: www.ng-newsletter.com/posts/validations.html section Custom validations
I don't need to check any specific field. Enough email, just the text (ie its presence and max(min)-length).
And here, as I understand it, we add its validation? - naomi.Hamill72 commented on October 3rd 19 at 04:18
There — Yes. If you need such simple things, then maybe it makes sense to use the standard classes ng-valid, ng-invalid and the built-in validators docs.angularjs.org/api/ng.directive:input? - nico.Hodkiewicz0 commented on October 3rd 19 at 04:21
October 3rd 19 at 04:17
Validation inputof should only do so via the ngModel Directive-dependent. 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 needs to explain. What is that worth? Reusability increased significantly, it is not necessary to fence their bicycles, all attached to scope and you can check the validity of fields referring to the property $valid as from the field itself and forms a whole.

Everything that concerns a whatever DOM manipulation should be made in the Directive. but onchange, and only need the ngModel controller or other directives.
Regarding adding class to all invalid fields or valid are added to specific fields. As I recall there are ready-made validators required attribute (add the class ng-invalid-require like) and the like have a validator emails. Out of the box, even to write nothing. - naomi.Hamill72 commented on October 3rd 19 at 04:20

Find more questions by tags Web DevelopmentAngularJavaScript