使用TextAngular plugin并试图customize a toolbar ,我尝试将自己的服务 (LinkService
) 注入(inject)到模块中,但收到 [$injector:unpr] 未知提供程序
错误。
module.config(function($provide, LinkService){
$provide.decorator('taOptions', ['taRegisterTool', '$delegate', function(taRegisterTool, taOptions){
// $delegate is the taOptions we are decorating
// register the tool with textAngular
taRegisterTool('colourRed', {
iconclass: "fa fa-square red",
action: function(){
this.$editor().wrapSelection('forecolor', 'red');
LinkService.createLink(/*...*/)
}
});
// add the button to the default toolbar definition
taOptions.toolbar[1].push('colourRed');
return taOptions;
}]);
});
如何将我的服务注入(inject)到此配置中?
最佳答案
我们无法将服务注入(inject)到configuration
block 中。
Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.
但是,我们可以将类似的逻辑制作成 provider 。我不确定 LinkService
的用法,但作为提供者,我可以看到类似以下内容...
module.provider('LinkProvider', function () {
var link;
return {
createLink: function (value) {
link = value;
},
$get: function () {
return {
link: 'http://' + link
}
}
}
});
module.config(function (LinkProvider) {
LinkProvider.createLink('stackoverflow.com');
});
查看博客 Differences Between Providers In AngularJS有关提供商的全面文章
关于javascript - 从 Angular JS 中的装饰器内部调用工厂服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984651/