本文介绍了未调用链接函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指令定义如下 -

I have a directive defined as following -

.directive('codesection', ['$compile', function ($compile) {
  return {
      restrict: 'E',
      scope: { current: '=', parent: '=', index: '=', params: '=' },
      controller: ['Messages', '$scope', 'Modals', 'framewidth', '$http', '$rootScope', function (Messages, $scope, Modals, framewidth, $http, $rootScope) {
          //code
      }],
      link: function (scope, element, attr) {
          element.bind('mouseover', function (ev) {
              ev.stopPropagation();

              var wrappers = angular.element(document.getElementsByClassName('codesection'));
              angular.forEach(wrappers, function (value, key) {
                  angular.element(value).children('span').removeClass('br');
              });

              element.children('.codesection').children('span').addClass('br');
          });

      },
      compile: function (tElement, tAttr, transclude) {
          var contents = tElement.contents().remove();
          var compiledContents;
          return function (scope, iElement, iAttr) {
              if (!compiledContents) {
                  compiledContents = $compile(contents, transclude);
              }
              compiledContents(scope, function (clone, scope) {
                  iElement.append(clone);
              });

          };
      },
      templateUrl: './partials/directives/codesection.html',
      replace: true
  }
}])

我遇到的问题是从未调用过链接函数.谢谢!

The issue I am having is that Link functions is never called. Thank you!

附言编译逻辑的原因是指令是递归的.

P.S. The reason for the Compile logic is that the directive is recursive.

推荐答案

如果你的意思是 link: function (scope, element, attr) { 没有被调用,那么很明显:编译函数已经返回一个链接函数.什么被定义为 link: 不再重要并且被忽略.

If you mean that link: function (scope, element, attr) { isn't called then it's pretty clear: The compile function already returns a link function. What is defined as link: doesn't matter anymore and is ignored.

这篇关于未调用链接函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:24