是否有可能将服务注入到Angular 1.5组件的templateUrl属性中?(Is it possible to inject a service into Angular 1.5 compone

编程入门 行业动态 更新时间:2024-10-10 09:24:51
是否有可能将服务注入到Angular 1.5组件的templateUrl属性中?(Is it possible to inject a service into Angular 1.5 component's templateUrl property?)

我的指令需要一个常量(MODULE_ROOT_URL)来生成模板路径。 通过指令语法,我可以将常量注入到目录工厂函数中。 我如何将此指令转换为Angular 1.5组件? 是否有可能将服务注入到Angular 1.5组件中?

谢谢。

更新:我知道该服务可以注入组件控制器。 但是,如何为组件的templateUrl属性注入服务?

Update2:请参阅plnkr。 我创建了指令和组件版本。 指令版本正常工作。 但组件版本有错误[ngTransclude:orphan]

https://plnkr.co/edit/DMumuIpXJY6RDCX6XObz?p=preview

angular.module('AbcModule') .directive('abcDirective', ['MODULE_ROOT_URL', function (MODULE_ROOT_URL) { return { restrict: 'E', templateUrl: MODULE_ROOT_URL + 'abc/abc.tpl.html' } }]);

My directive needs a constant (MODULE_ROOT_URL) to generate the template path. With directive syntax, I can inject the constant to the directory factory function. How can I convert this directive to Angular 1.5 components? Is it possible to inject a service into Angular 1.5 components?

Thanks.

Update: I know the service can be injected into the component controller. But how can I inject a service for a component's templateUrl property?

Update2: Please see plnkr. I create both of directive and component version. The directive version works fine. But the component version has error [ngTransclude:orphan]

https://plnkr.co/edit/DMumuIpXJY6RDCX6XObz?p=preview

angular.module('AbcModule') .directive('abcDirective', ['MODULE_ROOT_URL', function (MODULE_ROOT_URL) { return { restrict: 'E', templateUrl: MODULE_ROOT_URL + 'abc/abc.tpl.html' } }]);

最满意答案

templateUrl和template可以是可以注入的函数和依赖关系。

angular.module('AbcModule') .component('abcDirective', { restrict: 'E', templateUrl: function(MODULE_ROOT_URL) { return MODULE_ROOT_URL + 'abc/abc.tpl.html'; } });

templateUrl and template can be functions and dependencies can injected.

angular.module('AbcModule') .component('abcDirective', { restrict: 'E', templateUrl: function(MODULE_ROOT_URL) { return MODULE_ROOT_URL + 'abc/abc.tpl.html'; } });

更多推荐

本文发布于:2023-08-08 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有可能   组件   属性   Angular   templateUrl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!