1

I'm trying to create a directive so that I can do some element manipulation upon the load of an iFrame inside of an ng-repeat.

The below CodePen contains an iframe both inside and outside of an ng-repeat. The directive initializes without a problem but the load events never fire.

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

Any pointers greatly appreciated.

2 Answers 2

3

You can use angular element ready method for that. Try the following please.

app.directive('iframeOnload', [function(){
return {
    scope: {
        callBack: '&iframeOnload'
    },
    link: function(scope, element, attrs){
      alert("Directive Init")
        element.ready(function(){
          alert("Load Event Fired")
            return scope.callBack();
        })
    }
}}])
Sign up to request clarification or add additional context in comments.

Comments

0

The reason for this is due to the fact that you are not using jQuery, therefore AngularJs uses it's own jqLite, from the docs.

on() - Does not support namespaces, selectors or eventData

You can use the native addEventListener on the DOM node, so your link function will like that:

link: function (scope, $element, attrs) {
  console.log('Directive Init');
  var element = $element[0];
  element.addEventListener('load', function () {
    console.log('Load Event Fired');
    return scope.callBack();
  }, { once: true });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.