1

I am trying to bind an event to an iframe so that no matter where in the iframe is clicked, the event fires a function in the angular controller. I am trying to do this with just the jquery lite that ships with angular, or with pure javascript, whichever solution I happen across first.

I have been trying variations on this but not having any luck:

angular.element('iframe').contents().find('html').bind('click', function () {
    alert("hello");
});
2
  • Can you post a fiddle? Commented Jul 28, 2014 at 14:04
  • Here is one with full jquery i can deal with a solution using the entire jquery library if necessary plnkr.co/edit/wNUEbteVkRa0ZotQEY2w?p=preview Commented Jul 28, 2014 at 14:20

1 Answer 1

3

Well, you can't go looking for elements out of the blue like normal jquery apps. you got to use a directive.

look here : plunker demo

In myDirective, i use the iframe element to do what you wanted:

app.directive('myDirective', function(){

  return {
    link : function(scope, element, attributes){
      element.contents().find('html').bind('click', function () {
           alert("hello");
       });
    }
  };

});

You should also take some steps to ensure that the 'element' in the link function is an iframe.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this does work in plnkr but not in my app unfortunately so I am guessing there is something blocking the click even in what is being loaded in the iframe

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.