小编典典

角度-装饰指令

angularjs

我正在尝试使用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

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


阅读 228

收藏
2020-07-04

共1个答案

小编典典

本文说明了实际上如何将带指令的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/

2020-07-04