gpt4 book ai didi

javascript - 使用 Angular 的 $compile 时在脚本标签中添加 Controller

转载 作者:行者123 更新时间:2023-11-30 05:36:55 25 4
gpt4 key购买 nike

$compile -ing 一个 Angular HTML 模板字符串我试图将 其他 Controller 和指令放入 <script>标记并在 HTML 模板中使用它们。

通过这种方式,我实际上是在尝试实现某种插件机制,以便我可以加载外部文件来增强我的应用程序的功能。

<script>标签确实得到了评​​估,但我的问题是,HTML 模板编译发生在 JavaScript 评估之前。所以编译器提示缺少 Controller 。


例子:

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

这个例子抛出一个错误:Error: Argument 'Controller' is not a function, got undefined

如果删除 app.js 中的第 15-18 行(引用创建的 Controller 的 HTML) ,您可以在控制台中看到以这种方式创建 Controller 确实有效。

最佳答案

我终于根据Loading an AngularJS controller dynamically中的解决方案做到了.感谢@JoseM、@MaximShoustin 和@JussiKosunen 的提示和帮助。

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

关于javascript - 使用 Angular 的 $compile 时在脚本标签中添加 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23134725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com