码迷,mamicode.com
首页 > Web开发 > 详细

Angularjs: call other scope which in iframe

时间:2018-04-13 11:29:48      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:wrapper   cross   star   detail   href   als   unity   exp   reference   

Angularjs: call other scope which in iframe

 

------------------------------------------------------------------------------------------------------------

To access and communicate in two directions (parent to iFrame, iFrame to parent), in case they are both in the same domain, with access to the angular scope, try following those steps:

*You don’t need the parent to have reference to angularJS library…

Calling to child iFrame from parent

1.Get child iFrame element from the parent (link to answer):

document.getElementById("myIframe").contentWindow

2.Access the scope of the element:

document.getElementById("myIframe").contentWindow.angular.element("#someDiv").scope()

3.Call the scope’s function or property:

document.getElementById("myIframe").contentWindow.angular.element("#someDiv").scope().someAngularFunction(data);

4.Call $scope.$apply after running the logic of the function/updating the property (link to Mishko’s answer):

$scope.$apply(function () { });

  • Another solution is to share the scope between the iFrames, but then you need angular in both sides: (link to answer and example)

Calling parent from child iFrame

  1. Calling the parent function:

parent.someChildsFunction();

Will update also on how to do it cross domain if it is necessary..

 

You should be able to get parent scope from iFrame:

var parentScope = $window.parent.angular.element($window.frameElement).scope();

Then you can call parent method or change parent variable( but remember to call parentScope.$apply to sync the changes)

Tested on Angular 1.3.4

Angularjs: call other scope which in iframe

标签:wrapper   cross   star   detail   href   als   unity   exp   reference   

原文地址:https://www.cnblogs.com/oxspirt/p/8818049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!