My component is based on a template referred to via templateUrl
. This template contains a script
tag. However, the script in this tag is not executed. Is there a way to tell AngularJS 2 to do that?
Asked
Active
Viewed 147 times
2

gicig
- 334
- 3
- 14
-
out of mere curiosity, would you mind giving a relevant extract of this template? – Sebas Dec 13 '16 at 15:13
-
`` – gicig Dec 13 '16 at 15:14
-
innerHTML is not something that will make the script execute. Doing it with jQuery's `html()` method will. cuz what they are doing is extracting the script tag create a new one and append it to the head - related: http://stackoverflow.com/questions/2592092/executing-script-elements-inserted-with-innerhtml – Endless Dec 13 '16 at 15:15
-
2You should probably move it outside of the template as shown here - http://stackoverflow.com/questions/34140065/script-tag-in-angular2-template-hook-when-template-dom-is-loaded – dmoo Dec 13 '16 at 15:16
-
1http://stackoverflow.com/a/35570910/1291428 seems like the way to go – Sebas Dec 13 '16 at 15:20
-
1Thanks for useful hints, but this does not solve the problem I have, because the script **has to be retrieved from the server** not embedded in the AngularJS 2 component. The thing is that, in my case, the template is generated on the server side dynamically, along with the embedded `script`. It means that I must not _hardcode_ the JS code, as is the case with `ngAfterViewInit`. I use the term _hardcode_, because when I use `ngAfterViewInit`, the script can not be changed anymore, whereas in my case it can. – gicig Dec 13 '16 at 16:14
-
1I agree with @akaRoga, because sometimes in a bigger corporate architecture, there is this need to create scripts dynamically. I guess it's better to customize Angular 2 to match corporate architecture, rather than vice versa. – Saeed Neamati Mar 31 '17 at 07:32