Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
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> – maurycy Dec 15 '15 at 22:07
    
it is not recommended to use ng-init. better to asign data into the $scope in the controller side – AlainIb Dec 15 '15 at 22:13

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.