Angular ng-init: "entering data through server scripts"

From Angular docs regarding ng-init:

This directive may be abused to add unnecessary amounts of logic to your templates. There are only a few suitable uses of ngInit, such as for smoothing the special properties of ngRepeat, as seen in the demo below; and for entering data through server scripts.

Can someone explain the second use case:

... and for entering data through scripts on the server side.

In particular, would it be advisable to use this directive to pass a value (for example, a model identifier) ​​from a Rails / PHP view to an Angular controller?

Other methods (using $ location) seem unnecessarily complicated.

+4
source share
1 answer

That's right. If your PHP or Rails application creates a template, and it is convenient (and safe) to transfer data from this application to your Angular application as the value of the ng-init attribute, this is a valid use case.

It is more useful to make a $ http request if the data is a complex object and / or may change (or will be changed by the client application).

+3
source

Source: https://habr.com/ru/post/1620207/


All Articles