Adding controller in script tag when using angular's $compile

我们两清 提交于 2019-12-10 23:58:05

问题


When $compile-ing an angular HTML template string I'm trying to put additional controllers and directives inside a <script> tag and use those in the HTML template.

This way I'm essentially trying to implement some sort of plug-in-mechanism, so that I can load external files that augment my app's functionality.

The <script> tag does actually get evaluated, but my problem is, that the HTML template compilation takes place before the evaluation of the JavaScript. So the compiler complains about missing controllers.


Example:

http://plnkr.co/edit/8oZYhRHAjP84ecnl6hG3?p=preview

This example throws an error: Error: Argument 'Controller' is not a function, got undefined

If you delete lines 15-18 (the HTML that references the created conroller) in app.js, you can see in the console that creating a controller this way does actually work.


回答1:


I finally managed to do it based on the solution in Loading an AngularJS controller dynamically. Thx @JoseM, @MaximShoustin and @JussiKosunen for your hints and help.

http://plnkr.co/edit/fzmEZlP6bGBJqTOkfApH?p=preview



来源:https://stackoverflow.com/questions/23134725/adding-controller-in-script-tag-when-using-angulars-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!