Angularjs:调用 iframe 中的其他范围 [英] Angularjs: call other scope which in iframe

查看:28
本文介绍了Angularjs:调用 iframe 中的其他范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试中,给定了 2 个文档,A 和 B.在 A 文档中,有一个 iframe,iframe 源是 B 文档.我的问题是如何修改B文档的某个范围的变量?

这是我的代码:一个文档

<头><meta charset="utf-8"><title>Google 手机图库</title><script type='text/javascript' src="js/jquery-1.10.2.js"></script><script type='text/javascript' src="js/angular1.0.2.min.js"></script><脚本>变量 g ;功能测试($scope,$http,$compile){$scope.tryget = function(){var iframeContentWindow = $("#iframe")[0].contentWindow;var iframeDOM = $("#iframe")[0].contentWindow.document;var target = $(iframeDOM).find("#test2");var iframeAngular = iframeContentWindow.angular;var iframeScope = iframeAngular.element("#test2").scope();iframeScope.parentcall();iframeContentWindow.angular.element("#test2").scope().tempvalue = 66 ;iframeScope.tempvalue = 66;iframeContentWindow.tt = 22;iframeScope.parentcall();控制台日志(iframeScope.tempvalue);console.log(angular.element("#cont").scope());}}<身体><div ng-controller="测试"><div id="cont" ><button ng-click="tryget()">try</button>

<iframe src="test2.html" id="iframe"></iframe></html>

我的B文件:

<html lang="en" ng-app=""><头><meta charset="utf-8"><title>Google 手机图库</title><script type='text/javascript' src="js/jquery-1.10.2.js"></script><script type='text/javascript' src="js/angular1.0.2.min.js"></script><脚本>无功 tt = 11;功能 test2($scope,$http,$compile){console.log("test2 控制器初始化");$scope.tempvalue=0;$scope.parentcall = function(){$scope.tempvalue = 99 ;console.log($scope.tempvalue);控制台日志(tt);}}<身体><div ng-controller="test2" id="test2"><div id="cont" ><button ng-click="parentcall()">获取脚本</button>

{{临时值}}

</html>

注意:实际上有一些方法可以做到这一点,我觉得这就像一个黑客而不是完成它的正确方法:即在 b 文档中创建一个按钮,然后使用 angularjs ng-click 绑定.之后A文档jquery触发"点击按钮.

解决方案

在两个方向(父到 iFrame,iFrame 到父)访问和通信,以防它们都在同一个域中,可以访问 angular 范围,请尝试执行以下步骤:

*您不需要父级引用 angularJS 库...

从父级调用子 iFrame

1.从父级获取子 iFrame 元素 (回答链接):

<块引用>

document.getElementById("myIframe").contentWindow

2.访问元素的作用域:

<块引用>

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

3.调用作用域的函数或属性:

<块引用>

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

4.在运行函数逻辑/更新属性后调用 $scope.$apply (链接到 Mishko 的答案):

<块引用>

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

从子 iFrame 调用父级

  1. 调用父函数:

<块引用>

parent.someChildsFunction();

如有必要,还将更新如何跨域进行..

In my test, given 2 document, A and B. In A document, there is an iframe, the iframe source is B document. My question is how to modify B document certain scope of variable?

Here is my code: A document

<html lang="en" ng-app="">
<head>
  <meta charset="utf-8">
  <title>Google Phone Gallery</title>
  <script type='text/javascript' src="js/jquery-1.10.2.js"></script>
  <script type='text/javascript' src="js/angular1.0.2.min.js"></script>
  <script>
  var g ;
function test($scope,$http,$compile)
{
    $scope.tryget = function(){

        var iframeContentWindow = $("#iframe")[0].contentWindow;
        var iframeDOM = $("#iframe")[0].contentWindow.document;
        var target = $(iframeDOM).find("#test2");
        var iframeAngular = iframeContentWindow.angular;
        var iframeScope = iframeAngular.element("#test2").scope();
        iframeScope.parentcall();
        iframeContentWindow.angular.element("#test2").scope().tempvalue = 66 ;
        iframeScope.tempvalue = 66;
        iframeContentWindow.tt = 22;
        iframeScope.parentcall();
        console.log(iframeScope.tempvalue);
        console.log(angular.element("#cont").scope());
    }
}

  </script>
</head>
<body>

<div ng-controller="test">
        <div id="cont" >
            <button ng-click="tryget()">try</button>
        </div>
</div>
<iframe src="test2.html" id="iframe"></iframe>

</body>
</html>

My B document:

<!doctype html>
<html lang="en" ng-app="">
<head>
  <meta charset="utf-8">
  <title>Google Phone Gallery</title>
  <script type='text/javascript' src="js/jquery-1.10.2.js"></script>
  <script type='text/javascript' src="js/angular1.0.2.min.js"></script>
  <script>
var tt =11;
function test2($scope,$http,$compile)
{
    console.log("test2 controller initialize");
    $scope.tempvalue=0;
    $scope.parentcall = function()
    {
        $scope.tempvalue = 99 ;
        console.log($scope.tempvalue);
        console.log(tt);
    }
}

  </script>
</head>
<body>

<div ng-controller="test2" id="test2">
        <div id="cont" >
            <button ng-click="parentcall()">get script</button>
        </div>
        {{tempvalue}}
</div>

</body>
</html>

Note: Actually there is some way to do it, which i feel it like a hack instead of proper way to get it done: that is create a button in b Document, and then bind with angularjs ng-click. After that A document jquery "trigger" click on button.

解决方案

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 () { });

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..

这篇关于Angularjs:调用 iframe 中的其他范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆