Angularjs: call other scope which in iframe

Posted 牛顿的小脑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Angularjs: call other scope which in iframe相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

AngularJS指令没有被调用

angularJS 事件广播与接收

call,apply,bind 实现

AngularJS 监视对象数组以进行数据更改

如何在 Angularjs 函数中使用 Popover

如何在 Web 表单中实现 AngularJS 控制器?