在 Angular JS 中从装饰器内部调用工厂服务
Calling factory service from inside decorator in Angular JS
使用 TextAngular plugin and trying to customize a toolbar,我试图将我自己的服务 (LinkService
) 注入模块,但出现 [$injector:unpr] Unknown provider
错误。
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;
}]);
});
如何将我的服务注入到这个配置中?
我们无法将服务注入 configuration
块。
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('whosebug.com');
});
查看博客 Differences Between Providers In AngularJS 以全面了解提供商
使用 TextAngular plugin and trying to customize a toolbar,我试图将我自己的服务 (LinkService
) 注入模块,但出现 [$injector:unpr] Unknown provider
错误。
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;
}]);
});
如何将我的服务注入到这个配置中?
我们无法将服务注入 configuration
块。
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('whosebug.com');
});
查看博客 Differences Between Providers In AngularJS 以全面了解提供商