3

I have an iframe nested in my app that has an angular project in it.

How can I get access to the scope inside that?

asked Jun 5, 2014 at 17:35
0

3 Answers 3

1

The magic is in <iframe>.contentWindow

When going from the parent to the child iframe (which is what you do when debugging), you'll want get the child window object, and use its definition of angular.

var w = $('iframe')[0].contentWindow;
w.angular.element(<selector>).scope();

Note that this does work with chrome's development shortcuts, so you can do

w.angular.element(0ドル).scope()

to get the scope of the currently selected element.

EDIT:

You can also directly assign the parent window's angular object to the child's definition (though I wouldn't recommend it if the parent is using angular itself)

window.angular = w.angular

This makes the parent effectively transparent, so you can use angular as normal

angular.element(0ドル).scope()

and it has the advantage of fixing angular console tools like Batarang

Note: the iframe must be from the same origin in order for this to work. For instance, trying this in plunker won't work as the iframe is generated dynamically, and has a source of "about:blank".

answered Oct 16, 2014 at 21:19
2
  • This gives me a error DOMException: Blocked a frame with origin "http://plnkr.co" from accessing a cross-origin frame on chrome. Any Idea? Commented Feb 28, 2016 at 10:14
  • @pravin the iframe contents must be from the same origin. Within the same project, this usually isn't an issue. Commented Feb 28, 2016 at 18:28
0

Select iframe from debug options and do something like below:

jQuery('#my_element_id').scope();

To do this with javascript, I have setup a plunker link: http://plnkr.co/edit/whVo7wanhmcUfC7Nem9J?p=preview

Below js method access the scope of a element and passes to the parent method:

var passScopeToParent = function() {
 var scope = angular.element(jQuery("#myListItem")).scope();
 parent.change(scope);
}

parent widnow method accesses scope and changes its expressions:

var change = function(scope) {
 scope.$apply(function() {
 scope.department = 'Superhero';
 });
}

You can tweak this link to access the scope an element within iframe from parent window and print it in the console window.

answered Jun 5, 2014 at 18:33
3
  • any way to do this with javascript direct? Commented Jun 6, 2014 at 15:42
  • jQuery('#my_element_id').scope(); Only if you select the frame. Commented Jun 6, 2014 at 16:07
  • Your plunker doesn't actually demonstrate passing scope through an iframe. The javascript that executes gets the scope from its direct parent element (which it can do without element.scope via directives). It also doesn't pass it back through the iframe. Commented Oct 16, 2014 at 20:44
0

Late to the party. What I do is put a button in the directive and attach it to a function that prints the scope.

<button ng-click="echoScope()">Echo Scope</button>
$scope.echoScope = function() { console.debug($scope); };
answered Oct 18, 2015 at 16:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.