0

I am going through various angular examples, and sometimes I see angular developers increases a dummy variable like this below. Later they don't refer this variable in the controller scope. How should I interpret/read this?

<input on-keyup="count = count + 1">

Thanks.

Knight of Ni
  • 1,780
  • 3
  • 20
  • 47

1 Answers1

1

In the answer you reference, on-keyup is a user-defined directive, which is used as follows:

<input on-keyup="count = count + 1">

count = count + 1 is an expression that the directive executes using $eval when a keyup event is detected:

scope.$apply(attrs.onKeyup);

This will increment a count property on the $scope object. If the property does not exist, it is created. If this variable/property is not referenced by the controller, or the view, or logged, then it really is a dummy variable.

In this fiddle, I added {{count}} to the view.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • I see now. So there is no special underlying need to makes this adding in HTML. I made some checks and I could go with and make this calculation inside a controller http://jsfiddle.net/JNGGC/ . – Knight of Ni Aug 21 '13 at 22:13
  • 1
    @flyer, just FYI, you can simplify your fiddle a bit further: `` and `scope.$apply();`. – Mark Rajcok Aug 21 '13 at 23:55
  • In other hand if I extend your fiddle to http://jsfiddle.net/zhYAz/2/ it makes things more complex when we look at the console (`prev` and `next` are different then applied to bound view). I would expect the `prev` is always less than `next`. – Knight of Ni Aug 22 '13 at 01:02
  • 1
    @flyer, you are changing `scope.next` after calling $apply, so the view will not be updated with the change. You'll need to call $apply again: http://jsfiddle.net/mrajcok/zhYAz/3/ – Mark Rajcok Aug 22 '13 at 03:01
  • Ohh. my $scope.next was updated every next call. That's make sens. Thank you. – Knight of Ni Aug 22 '13 at 11:05