簡體   English   中英

覆蓋角度指令鏈接

[英]Override Angular Directive link

我對Angular的了解有限,所以請多多包涵。 我處於只能修改所有Angular內容之前包含的一個js文件的情況。 有一個指令導致了問題,但是我不能直接修改它。 因此,我嘗試通過在文檔就緒塊中添加以下代碼段來覆蓋它:

app.directive('selectionChange', function($rootScope){
   return {
      priority: 1,
      terminal: true,
      link: function(scope, el, attr) {
         console.log('works');
      };
   };
});

我可以看到此指令已添加到invokeQueue的末尾,但從未執行過。 我如何獲得此附件? 謝謝!

更新:

抱歉,讓我嘗試澄清一下。 問題是,原始指令將繼續執行,而新附加的指令則不會執行(通過使用console.log和alert進行了測試)。 標記是這樣的:

<html>
<head>
...
<script src="[the file I can modify].js"></script>
...
</head>
<body>
...
<script src="angular.js"></script>
<script src="directives.js"></script> // here is where the existing selectionChange directive is defined
...
</body>
</html>

這是一個小矮人

$(function () {
  var app = angular.module('app');

  app.config(function ($provide) {
    $provide.decorator('badDirective', function ($delegate) {
      var badDirective = $delegate[0];

      var link = function (scope, element) {
        element.text('good');
      }

      var originalCompile = badDirective.compile || function () {};
      badDirective.compile = function () {
        originalCompile.apply(badDirective, arguments);

        // compile returns link fn, directive 'link' property will be ignored anyway
        return link;
      }

      return $delegate;
    });    
  });
})

在“就緒”狀態(例如jQuery ready實現)上進行操作是正確的。 這樣,代碼將在引導過程之前啟動(加載angular.js后,它將通過ng-app在“就緒”隊列中)。

bad指令在內部只是badDirective服務,其中包含一個DDO數組(因為可能有多個具有相同名稱的指令)。 它可以像其他任何服務一樣被裝飾。

可以使用linkcompile (可以返回鏈接)DDO屬性來定義鏈接函數。 第二個覆蓋第一個,因此在修飾指令時始終堅持compile

在該文件中,您可以修改對新指令具有(引用/定義)的腳本標記,並將該標記放在出現問題的指令定義之后的正文底部。 通過定義為最后一個,您將確保該指令是呈現的指令。

暫無
暫無

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

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