Link 函数在 AngularJS 指令中没有得到 运行

Link function not getting run in AngularJS directive

我在使用 angular 指令时遇到问题。它似乎没有 运行 link 功能。

我觉得这很明显,但我就是想不通。

从下面看,指令是必需的

angular.module('test').requires // ["injectedModule"]

代码如下。 Fiddle.

任何帮助都会很棒。

angular
    .module('test', ['injectedModule'])
    .controller('tester', [
        function() {
            this.test = function(data) {
                alert(data);
            }
        }
    ]);

angular
    .module('injectedModule', [])
    .directive('testing', [
      function() {
        return {
          restrict: 'E',
          scope: true,
          link: function(scope, element, attrs) {
            alert(scope, element, attrs);
          }
        };
      }
    ]);
<div ng-app="test">
    <div ng-controller="tester as t">
         <video id="test" ng-src="https://scontent.cdninstagram.com/hphotos-xfa1/t50.2886-16/11726387_1613973172221601_1804343601_n.mp4" testing="t.test(el)" />        
    </div>
</div>

在我看来像

restrict: 'E',

应该是

restrict: 'A',

你的指令根本没有被调用。

我认为错误在于您对指令的限制。 您将指令限制为仅匹配元素(换句话说就是标签)。您应该限制匹配属性 'A'。这是 angular 官方文档 https://docs.angularjs.org/guide/directive 这是你的 fiddle working

代码示例:

angular
.module('injectedModule', [])
.directive('testing', [
  function() {
    return {
      restrict: 'A',
      scope: true,
      link: function(scope, element, attrs) {
        alert(scope, element, attrs);
      }
    };
  }
]);