0

I am using node js for backend and angular for frontend. I have a scoped function inside of app.js that I would like to execute upon the html page loading. I can get the functionality I want if I add this logic behind a button, but ng-init is unsuccessful. By unsuccessful, I mean that it will do what I want only 10% of the time. How can I execute this function one time when the page loads and make sure that it persists?

This works

<button ng-click='setCurFiles("/Users/nick/Desktop/");'>test</button>

This works but is not reliable

<div ng-init='setCurFiles("/Users/nick/Desktop");'></div>
georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

0

The ng-init directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit.1

Put initialization code inside controllers:

app.controller("ctrl", function() {
    this.$ngInit = function() {
        setCurFiles("/Users/nick/Desktop/");
    };
    function setCurFiles(url) {
        //...
    }
})
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Ok. That makes sense but the url is constantly changing based on different views. I don't want to set a static directory inside of app.js. I want a modular function that can accept arguments from many different html files – Nick Dodson Feb 06 '20 at 23:08