用AngularJS指令覆盖img和a的默认行为

Overriding default behaviour for img and a with AngularJS directives

本文关键字:默认 AngularJS 指令 覆盖 img      更新时间:2023-09-26

我目前正试图为<a><img>编写自定义指令。我有一些测试代码,我想看到得到图像的src。但是,当我导航到带有img标签的页面时,下面的代码似乎不会触发。

知道为什么吗?

'use strict';
(function() {
  angular.directive('img', [function() {
    return {
      restrict: 'E',
      scope: {
        src: '='
      },
      link: function(scope, element, attrs) {
        console.log(scope.src);
      }
    };
  }]);
})();

我可以用这段代码拉出源代码。

var myApp = angular.module('myApp', []);
myApp.directive('img', [function () {
    return {
        restrict: 'E',
        link: function (scope, element, attrs) {
            console.log(attrs.src);
        }
    };
}]);