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?

azerafati
  • 18,215
  • 7
  • 67
  • 72

1 Answers1

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">.

dfsq
  • 191,768
  • 25
  • 236
  • 258