2

well, I don't know if something like this is possible or not? so let's just ask and find out.

<input name="link" ng-model="addItemLink" ng-class="{'input-blink': this.$pristine}" />

in general, do we ever get able to use keyword this in angular views?? and could you put me on a path to achieve something similar here??

P.S: I already know about normal angular form validation and how to properly access $pristine. I want to know about this here?? uh and I guess $pristine is totally undefined outside a form, isn't it?

1 Answer 1

1

do we ever get able to use keyword this in angular views

Yes, of course, however it's not used often, because it doesn't mean what we are used to think this points to. In Angular view directives (in attributes) this points to current $scope object and not current HTMLElement object.

So for example, you could fix your code using this (myForm name of the form):

<input name="link" ng-model="addItemLink" ng-class="{'input-blink': this.myForm.link.$pristine}" />

But in this case, this is pretty redundant. There might be rear situations when you need to use this, for example to use dynamic name of the model:

$scope.name = 'user';

and in HTML:

<input ng-model="this[name]">

is equivalent to manually hardcoded user as model <input ng-model="user">.

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

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.