Why in most cases do Angular docs recommend against ng-init?

From ng-init documentation :

The only suitable use of ngInit is for antialiasing the special ngRepeat properties, as shown in the demo below. Apart from this case, you should use controllers instead of ngInit to initialize values ​​in scope.

This document gives the following example:

<div ng-repeat="innerList in list" ng-init="outerIndex = $index">
    // [snip]
</div>

      

Which makes it advisable to use ng-init

with ng-repeat

, but not, for example, with a help ng-model

like this:

<input ng-model="thing.prop"
       ng-init="thing.prop = thing.prop || 'defaultValue'">

      

Doc says to "use controllers, not ngInit". What advantage does the controller offer in this case? Is this a stylistic preference for Angular, or are there cases where code like the one above won't work?

+3


source to share


1 answer


This is because the documentation should be as concise as possible, and if they create a controller in each example, it won't be as accurate and clear



-1


source







All Articles