Reputation: 161
*Disclaimer: I am an Angular noob.
This is a more a style/standards question. I have an AngularJs SPA app where the template pages each have some javascript associated with elements on the template.
If I load the js in the index.html then it errors because the element it is binding to is not loaded yet.
If I load the js in the bottom of the template file with a <script>
tag, it seems to be executed as a synchronous request because I get the "Synchronous XMLHttpRequest is deprecated" error in the console.
The solution to use $.getScript(url); was provided previously and that does seem to work.
However, many years of coding have taught me that when you have to hack something like that, you are probably violating some design rule.
Is there some design guidance about when/where/how to include javascript in an angular template when said javascript is bound to elements on that template?
Upvotes: 0
Views: 124
Reputation: 171669
For any dom related code use a directive. This assures that the element the directive is bound to exists at the time the code is run and will run for each instance of that directive
Upvotes: 1