编辑:jsFiddle示例在这里。
我有一个ngRepeat
生成包含的指令iframes
。
div(ng-repeat='element in elements')
ng-switch(on="element.type")
a-directive(ng-switch-when="something")
another-directive(ng-switch-when="somethingElse")
现在,在指令中,通过执行以下操作,我将在某些事件后将内容加载到iframe中:
$iframe[0].contentWindow.d_contents = "html"
$iframe[0].src = 'javascript:window["d_contents"]'
一切正常。
当我从模型中(在控制器中)删除这些元素之一时,如下所示:
elements.remove(object) //using sugarjs, that's not the issue, same behaviour with splice
用户界面会相应更新,即元素消失。
问题
这可以按预期工作:
elements.push(ele1)
elements.push(ele2)
.. init iframes inside ele1 and ele2 with content ..
elements.remove(ele2)
结果:ele2
从用户界面消失,ele1
但仍加载了iframe
这不是:
elements.push(ele1)
elements.push(ele2)
.. init iframes inside ele1 and ele2 with content ..
elements.remove(ele1)
结果:ele1
从UI消失,ele2
仍然与iframe一起存在,但是iframe内容恢复为空iframe.load()
并被解雇。
这是怎么回事 为什么我的iframe会重置?
您需要在load()内部添加加载逻辑,以便在DOM更改时重新加载内容。
var linkfn = function (scope, element, attrs) {
init(function () {
var $iframe = element.find("iframe")
var refresh = function () {
var doc = $iframe[0].contentWindow.document;
doc.open();
doc.write(scope.ele.source);
doc.close();
}
$iframe.load(function () {
console.log(scope.ele.id + ": loaded ");
//refreshing
refresh();
});
//initial loading
refresh()
})
}
您需要在doc.write()
这种情况下使用,否则您将获得递归循环错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句