4

From the Angular docs regarding ng-init:

This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting.

Could anyone explain the second use case:

...and for injecting data via server side scripting.

Specifically, would it be appropriate to use this directive to pass a value (e.g. model record id) from a Rails/PHP view to an Angular controller?

Other methods (using $location) seem unnecessarily complicated.

2
  • it's fairly simple, output of PHP is an HTML after all, so you can do something like: '<div class="blah_blah_blah" ng-init="variable=<?php echo $json_data;?>"></div> Commented Dec 15, 2015 at 22:07
  • it is not recommended to use ng-init. better to asign data into the $scope in the controller side Commented Dec 15, 2015 at 22:13

1 Answer 1

3

Absolutely. If your PHP or Rails app generates a template, and it is convenient (and secure) to pass data from that app to your Angular app as an ng-init attribute value, that is a valid use case.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.