我想要做的是,在插入DOM之前手动处理transclude并修改内容:
原文链接:https://www.f2er.com/angularjs/240574.htmlreturn { restrict: 'E',transclude: true,template: '<HTML>',replace: true,link: function(scope,element,attrs,ngModelCtrl,$transclude) { var caption = element.find('.caption'); $transclude(function(clone) { console.log(clone); clone.filter('li').addClass('ng-hide'); // this don't work clone.addClass('ng-hide'); // same this one clone.attr('ng-hide','true'); // same this one $compile(clone)(scope.$new()).appendTo(caption); caption.find('li').addClass('ng-hide'); // and this }); } }
在angular.js源代码中我找到了这个例子:
var templateElement = angular.element('<p>{{total}}</p>'),scope = ....; var clonedElement = $compile(templateElement)(scope,function(clonedElement,scope) { //attach the clone to DOM document at the right place }); //now we have reference to the cloned DOM via `clonedElement`
但是当我添加clonedElement.appendTo(标题);在内部链接功能中,它只在ng-repeat内添加注释.
我需要这个,因为在这种情况下我需要隐藏所有元素
<dropdown> <li ng-repeat="item in items"><a>{{item.label}}</a></li> </dropdown>
我需要在扩展ng-repeat之后在编译或DOM之前修改模板.之前会更好,因为我可以使用ng-hide指令而不是ng-hide类添加逻辑.