簡體   English   中英

角度 - 裝飾指令

[英]Angular - Decorating Directives

我正在嘗試使用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()。

您只需要包含“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];
}

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

使用decorator方法創建的decorator僅用於服務 必須使用servicefactoryprovidervalue創建它們。 請參閱此處的文檔。

如果要裝飾指令,可以使用相同的名稱創建另一個指令。 編譯DOM時將使用這兩個指令,您可以使用優先級定義編譯順序。

或者,如果您能夠修改使用您嘗試裝飾的指令的代碼,那么您可以創建一個在其模板中使用原始指令的新指令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM