0

I have few queries. Pls find them below -

1) A function defined inside an ng-init like given below

ng-init='function a() {}'

errors out. Changing the syntax to variable declaration type or Instantly invoked also doesn't work. why? Since we can anyway declare a variable, object, array. Why not a function?

2) Is a $watch created for all variables tied to a scope, OR is it created to only those scope variables which are shown in the view?

3) If you run the fiddle 'http://jsfiddle.net/Lvc0u55v/5753/', there is >10 $digest iterations error. This is expected. Now please comment and uncomment as given in the fiddle. there is no error, how come? here also $scope.a's value changes infinitely right?

anand patil
  • 507
  • 1
  • 9
  • 26

1 Answers1

1

Let me try to get your questions answered.

1) As far as I know ng-init is not supposed to work with function expressions. It's rather used to handle logical expressions. You may take a look into the docs, it's also pointed out there including a short example. So as the doc says:

The ngInit directive allows you to evaluate an expression in the current scope.

2) Generally a $watch is not tied up to each variable of a scope (even though it can be tied up to a whole digest cycle). As you've done in your example, you have bound $watch to your scopes variable called a. So it will trigger every time your $scope.a variable changes. You may also check the docs here too.

3) Regarding this question, the answer is pretty simple. Let's assume we start with $scope.a = 10 (as you've already done). The moment you run your app your $watch will pretty much fire. Doing so, you'll get the following:

nv = 10;
ov = 10;
$scope.a = ov * 9;

Assuming that, you'r $scope.a will now be 90, which obviously will fire your watcher again. This time with the following:

nv = 90;
ov = 10;
$scope.a = ov * 9;

Now, at this point your new value is the same as it was before. At this rate your watcher won't fire again because the value doesn't change (since it's exactly the same as before).

On the other hand, running $scope.a = nv * 9 would always update $scope.a and this will lead to an infinite loop.

I hope this helps.

Aer0
  • 3,792
  • 17
  • 33
  • For point#2, If i had declared a variable $scope.b in my controller and not tied to view like {{b}}, then is a watcher tied to 'b'? – anand patil Jun 22 '16 at 08:24
  • No, it's not. A watcher is just a listener which can be bound to a specific object. You're achieving that by doing $scope.$watch(OBJECT-HERE, ...). In your fiddle you've bound a watcher to your very specific $scope.a variable by adding 'a' into your watcher. – Aer0 Jun 22 '16 at 08:30
  • Oh, k...my bad that i did not explicitly ask. Please refer jsfiddle - http://jsfiddle.net/Lvc0u55v/5762/. In this code there are no explicit watchers. But watchers would be added by Angular right? So my question is will watchers be added by angular only to the variables 'name' and 'company' or even 'age'? Hope i am clearer. – anand patil Jun 22 '16 at 09:12
  • I guess you're question is about the curly braces {{ }}, am I right? That's Angulars way of two way data binding. This, basically, isn't a 'watcher' like $watch. Two way data binding is just a way of printing out the value which is inside of your variable (in your case now name and company). Doing so, Angular internally attatches a watcher to your variables so it can resolve their result and update your view accordingly. – Aer0 Jun 22 '16 at 09:20
  • I am so close to the answer :) So from your answer, I get that angular internally attaches watchers to name and company, cool! Now another question we missed answering was, is an internal watcher added to "age" since that is in the scope, but not in the view? – anand patil Jun 22 '16 at 09:22
  • In short my question was, is an internal watcher tied to only those variables which are 1) tied to the scope and 2) shown in a view? or is an internal watcher added to any variable tied to the scope only but not shown in a view? – anand patil Jun 22 '16 at 09:24
  • To be clear - I'm not 100 % sure about this, BUT I would assume not since you are not usind two way data binding to display your value inside of your view. That should be corret, I guess, since while using one way data binding Angular doesn't create a new watcher for the variable/object itself. So an internal watcher should only be tied up with variables wich are also present in your view due two way data binding -> {{ name }} – Aer0 Jun 22 '16 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115290/discussion-between-aer0-and-anand-patil). – Aer0 Jun 22 '16 at 10:03