我想要做的,是通过手工来处理transclude和修改的内容之前,我插入到DOM:如何在编译指令之前修改transcluded内容?
return {
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(caption);
时,它只在ng-repeat里面添加注释。
我需要这个,因为我需要隐藏在这种情况下
<dropdown>
<li ng-repeat="item in items"><a>{{item.label}}</a></li>
</dropdown>
我需要之前修改模板编译或DOM NG-重复扩大后的所有元素。之前会更好,因为我将能够使用ng-hide指令而不是ng-hide类来添加逻辑。