【发布时间】:2017-02-03 09:00:22
【问题描述】:
我在尝试手动使用 angular.injector 注入正在打开对话框的服务时遇到问题,而该对话框又在其模板中使用了一个指令,该指令使用动态模板。
我在控制台中遇到的错误是:
1:未知提供者:$rootElementProvider
2:找不到指令“ngInclude”所需的控制器“ngInclude”!
这里是plunker demonstrating the problem
var customSvc = angular.injector(['ng', 'pluginApp']).get("customSvc");
customSvc.testOpenDialog(100, scope);
我还尝试构建 url 并将其指定为指令属性并从 templateUrl 函数访问它,但在这种情况下它也失败了,因为我收到的值只是变量的名称,而不是内容。
如果我避免通过 angular.injector 注入服务,代码可以工作,但是由于应用程序的性质,我无法避免它,此外,我有兴趣了解此错误的背后原因是什么,如果有人好心解释一下这件事。
【问题讨论】:
-
你想在你的指令中访问“customSvc”服务,对吧?
-
我已经通过 angular.injector 函数访问它并且它可以工作,问题出在 sampleDirective 中,因为在那里,ng-include 不起作用
-
在sampleDirective中,为什么你在模板中使用ng-include,而你所包含的文件没有扩展名?template: " " , 使用 templareUrl:"sampleLinkTemplate.html"
-
因为它使用了链接函数中设置的范围内的变量值。这是一种基于作用域中存在的变量使用“动态模板”的方式
标签: angularjs angularjs-injector