我的指令定义如下 –
原文链接:https://www.f2er.com/angularjs/142524.html.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 } }])
附:编译逻辑的原因是该指令是递归的.