Angular - 装饰指令 [英] Angular - Decorating Directives
问题描述
我正在尝试使用 Angular 的装饰器"功能为某些指令添加功能.假设我的指令的名称是 myDirective.我的代码如下所示:
I'm trying to use Angular's "decorator" capability to add functionality to some directives. Assume that my directive's name is myDirective. My code looks like this:
angular.module('app').config([
'$provide', function($provide) {
return $provide.decorator('myDirective', [
'$delegate', '$log', function($delegate, $log) {
// TODO - It worked! Do something to modify the behavior
$log.info("In decorator");
}
]);
}
]);
我不断收到这条消息:
Uncaught Error: [$injector:unpr] Unknown provider: myDirectiveProvider from app
尽我所能,在装饰器函数运行时,指令已经注册.任何见解将不胜感激!
To the best of my ability, the directives are already registered by the time the decorator function runs. Any insight would be appreciated!
推荐答案
这篇文章展示了实际上如何使用带有指令的decorator().
This article shows how you can, in fact, use decorator() with directives.
您只需要包含指令"作为名称的后缀.因此,在我的例子中,我应该这样做
You just have to include "Directive" as the suffix for the name. Hence, in my example I should have been doing
return $provide.decorator('myDirectiveDirective', ['$delegate', '$log', function($delegate, $log) {
// TODO - It worked! Do something to modify the behavior
$log.info("In decorator");
// Article uses index 0 but I found that index 0 was "window" and index 1 was the directive
var directive = $delegate[1];
}
http://angular-tips.com/blog/2013/09/experiment-装饰指令/
这篇关于Angular - 装饰指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!