gpt4 book ai didi

angularjs - Angular - 装饰指令

转载 作者:行者123 更新时间:2023-12-03 23:26:24 25 4
gpt4 key购买 nike

我正在尝试使用 Angular 的“装饰器”功能为某些指令添加功能。假设我的指令的名称是 myDirective。我的代码如下所示:

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 

尽我所能,在装饰器函数运行时,指令已经注册。任何见解将不胜感激!

最佳答案

这篇文章展示了实际上如何将decorator() 与指令结合使用。

您只需要包含“指令”作为名称的后缀。因此,在我的例子中,我应该一直在做

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-decorating-directives/

关于angularjs - Angular - 装饰指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com