【发布时间】:2017-02-20 21:53:32
【问题描述】:
我想做的是在插入 DOM 之前手动处理 transclude 并修改内容:
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>
我需要在编译前修改模板或在 ng-repeat 展开后修改 DOM。之前会更好,因为我将能够使用 ng-hide 指令而不是 ng-hide 类添加逻辑。
【问题讨论】:
标签: angularjs angularjs-directive