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?
3 Answers 3
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".
-
This gives me a error
DOMException: Blocked a frame with origin "http://plnkr.co" from accessing a cross-origin frame
on chrome. Any Idea?pravin– pravin2016年02月28日 10:14:09 +00:00Commented 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.00500005– 005000052016年02月28日 18:28:41 +00:00Commented Feb 28, 2016 at 18:28
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.
-
any way to do this with javascript direct?user2167582– user21675822014年06月06日 15:42:41 +00:00Commented Jun 6, 2014 at 15:42
-
jQuery('#my_element_id').scope(); Only if you select the frame.bp4D– bp4D2014年06月06日 16:07:17 +00:00Commented 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.00500005– 005000052014年10月16日 20:44:38 +00:00Commented Oct 16, 2014 at 20:44
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); };