繁体   English   中英

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

[英]Overriding default behaviour for img and a with AngularJS directives

我目前正在尝试为<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);
        }
    };
}]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM