角度 - 装饰指令

bla*_*ter 13 angularjs angularjs-directive

我正在尝试使用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");
      }
    ]);
  }
Run Code Online (Sandbox Code Playgroud)

]);

我一直收到这条消息:

Uncaught Error: [$injector:unpr] Unknown provider: myDirectiveProvider from app 
Run Code Online (Sandbox Code Playgroud)

在我的能力范围内,指令在装饰器函数运行时已经注册.任何见解将不胜感激!

bla*_*ter 21

本文展示了如何使用带有指令的decorator().

您只需要包含"Directive"作为名称的后缀.因此,在我的例子中我应该这样做

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];
}
Run Code Online (Sandbox Code Playgroud)

http://angular-tips.com/blog/2013/09/experiment-decorating-directives/

  • 胡安,你有这个网址吗? (3认同)