0

I have an ng-init directive defined on my html tag, with it initializing some data. I want a certain JS function to run before this initialization occurs. How can I accomplish this? Basically my objective is to assign the data returned by this function to the variable initialized in ng-init.

Amal Antony
  • 6,477
  • 14
  • 53
  • 76
  • 3
    So why use `ng-init` at all? If you have a function that calculates the value of the variable, then why not simply assign the result of that function directly to a scope variable, in the controller itself? – Stewie Jan 23 '14 at 11:52
  • http://stackoverflow.com/questions/16150289/angularjs-running-initialization-code-when-view-is-loaded – ndpu Jan 23 '14 at 11:55
  • @Stewie, you are right, your comment helped me solve my problem. Thanks! – Amal Antony Jan 23 '14 at 16:22

1 Answers1

0

this is really bad approach but this should work.

  1. Write your method in another script file and include it before you are putting ng-init.
  2. Wrap it up as self executing function i.e.

    (function(){ //Your code })();

If you want to get the results, expose a global variable from the script file.

Anand
  • 14,545
  • 8
  • 32
  • 44