【发布时间】:2019-10-31 23:54:31
【问题描述】:
我正在尝试创建一个指令,以便在加载 ng-repeat 内的 iFrame 时进行一些元素操作。
下面的 CodePen 在 ng-repeat 的内部和外部都包含一个 iframe。指令初始化没有问题,但加载事件永远不会触发。
var app = angular.module('tester', []);
app.directive('iframeOnload', [function(){
return {
scope: {
callBack: '&iframeOnload'
},
link: function(scope, element, attrs){
alert("Directive Init")
element.on('load', function(){
alert("Load Event Fired")
return scope.callBack();
})
}
}}])
app.controller('MainCtrl', function($scope) {
$scope.tester1 = function (){
alert("Testing 1")
}
$scope.tester2 = function (){
alert("Testing 2")
}
$scope.theTemplate = [
{
Id: 1,
ElementId: 99,
Content: 'Element1',
ElementHeight: 130,
},
{
Id: 2,
ElementId: 98,
Content: 'Element2',
ElementHeight: 320,
},
{
Id: 3,
ElementId: 2,
Content: 'Element3',
ElementHeight: 320,
}];
});
.
<body ng-controller="MainCtrl">
<iframe iframe-onload="tester2()"></iframe>
<ul>
<li ng-repeat="element in theTemplate" id="li_{{element.Id}}" style="position: relative; height:{{element.ElementHeight}}px;">
<iframe iframe-onload="tester1()" scrolling="no" frameborder="0" style="width: 100%; height:{{element.ElementHeight}}px;" id="iframeElement{{element.Id}}"></iframe>
{{element.Content}}
</li>
</ul>
</body>
https://codepen.io/jibber4568/pen/agZxgP
非常感谢任何指针。
【问题讨论】:
标签: javascript jquery angularjs iframe angularjs-ng-repeat