我为drop.js创建了一个Angular包装器,它是弹出窗口的javascript库。
所有在drop指令中都很有效,包括正常的Angular绑定,除非使用ng-repeat。
这是一个截图,请注意弹出窗口的内容有一个绑定到控制器但是ng-repeat没有显示任何数据的值。
以下是完整的标题:http://plnkr.co/edit/plcUAWCRQ009blhY3nqF?p=preview
以下是一个示例用法:
<button>
button text {{someValue}}
<ul>
<li ng-repeat="thing in things">button text {{thing.code}} - {{thing.name}}</li>
</ul>
<drop classes='classes' position='position'>
<div>
Hello {{ $parent.someValue}}
<br>
<ul>
<!-- here is the problem, ng-repeat does not compile correctly -->
<li ng-repeat="thing in $parent.things">{{thing.code}} - {{thing.name}}</li>
</ul>
</div>
</drop>
</button>
这是指令代码:
.directive('drop', function ($compile) {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
classes: '=?',
constrainToScrollParent: '=?',
constrainToWindow: '=?',
position: '=?',
openOn: '=?'
},
template: '<div><div ng-transclude></div></div>',
link: function (scope, element, attrs) {
var drop;
var target = element[0].parentElement;
var compiled = $compile(element[0].children[0].innerHTML);
var initDrop = function() {
if (drop) {
drop.destroy();
}
// some default values go here if they weren't passed in....
drop = new Drop({
target: target,
content: compiled(scope)[0],
classes: scope.classes,
constrainToScrollParent: scope.constrainToScrollParent,
constrainToWindow: scope.constrainToWindow,
position: scope.position,
openOn: scope.openOn
});
}
initDrop();
// clean up element
element[0].innerHTML = '';
// some watchers go here....
}
}
});
答案 0 :(得分:1)
您可以尝试使用transcludeFn
。我不确定您是否需要pre
和post
功能,或者您是否可以将transclude
功能直接放在link
(postLink)中。 Plunker
.directive('drop', function ($compile) {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
classes: '=?',
constrainToScrollParent: '=?',
constrainToWindow: '=?',
position: '=?',
openOn: '=?'
},
link: {
pre: function(scope, element, attrs, ctrl, transclude){
transclude(scope, function(clone, scope) {
element.append(clone);
});
},
post:function (scope, element, attrs) {
var drop;
var target = element[0].parentElement;
var compiled = $compile(element[0].children[0]);
var initDrop = function() {
if (drop) {
drop.destroy();
}
if (typeof scope.classes == 'undefined') scope.classes = 'drop-theme-arrows-bounce';
if (typeof scope.constrainToScrollParent == 'undefined') scope.constrainToScrollParent = true;
if (typeof scope.constrainToWindow == 'undefined') scope.constrainToWindow = true;
if (typeof scope.position == 'undefined') scope.position = 'top center';
if (typeof scope.openOn == 'undefined') scope.openOn = 'click';
drop = new Drop({
target: target,
content: compiled(scope)[0],
classes: scope.classes,
constrainToScrollParent: scope.constrainToScrollParent,
constrainToWindow: scope.constrainToWindow,
position: scope.position,
openOn: scope.openOn
});
}
initDrop();
// clean up element
element[0].innerHTML = '';
...
}
}
}
});