我想要做的,是通過手工來處理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類來添加邏輯。