Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ng-init watch over change on instantiated property like ng-model does?

Does ng-init watch over change on instantiated property like ng-model does?

Apparently not, so I set a watch as shown below:

app.js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.$watch('myProp1', function(newVal, oldVal){
    $scope.myProp1 = newVal
  })
});

html

<body ng-controller="MainCtrl">
<input ng-init='myProp="my property"'>{{myProp}}</br>
<input ng-init='myProp1="my 1 property"'>{{myProp1}}</br>
<input ng-init='myProp11="my 11 property"' ng-model='myProp11'>{{myProp11}}

Here is plnkr

  1. Does ng-init watch over change on instantiated property like ng-model does?
  2. How do I watch the changes in the property instantiated by ng-init?
  3. What's wrong with the $watch function above?
like image 246
Indyarocks Avatar asked Aug 29 '14 18:08

Indyarocks


People also ask

How does ng-init work?

The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.

Why do we use ng-init in the following code?

Ng-init directive is used to initialize a variable, which will allows evaluating an expression in given scope.

When Ng-INIT is called?

In Angular 1. x, ngInit is called when template is re-rendered. In other words “ng-init” is called, when I take turns back to a page. In Angular2, there is no “ng-init” but we can create a ways like this using the directive and ngOnInit class.

What is NG model options in AngularJS?

The ng-model-options directive is used to control the binding of an HTML form element and a variable in the scope. You can specify that the binding should wait for a specific event to occur, or wait a specific number of milliseconds, and more, see the legal values listed in the parameter values below.


1 Answers

Does ng-init watch over change on instantiated property like ng-model does?

No, It is only to initialize a property on the scope. I would recommend not to use it. Markup is not for variable initialization, you should do it in your controller as much as possible.

How do I watch the changes in the property instantiated by ng-init?

You can watch just like watching any other property, but watching it does not mean that watch will get triggered. Watches are triggered only during the digest cycle and digest cycle gets invoked only if angular has something to do with a particular action.

What's wrong with the $watch function above?

Your watch on prop1 will never get executed (after ng Init initialization), because you are never changing the model value since there is no ngModel bound to it. And there is no angular action is done on the element and hence digest cycle will not happen.

As an example just attach a keyup event on the element, and assign the value of input to the property myProp, because you have registered keyup handler it will trigger digest cycle after the handler is run, and you will see the the watch getting executed and binding getting updated

 <input ng-init='myProp="my property"' ng-keyup="test(myProp=$event.target.value)">{{myProp}}

plnkr

like image 156
PSL Avatar answered Sep 23 '22 03:09

PSL