如何在 AngularJS 中将值从弹出窗口交换到父窗口 [英] How to exchange a value from pop-up to parent window in AngularJS

查看:24
本文介绍了如何在 AngularJS 中将值从弹出窗口交换到父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要动态交换数据的场景(从父母到孩子反转(父母到孩子) ).我参考了几个链接,它帮助我将数据从 PARENT 传递给了孩子,但是从孩子到父母没有.

下面我有父到子的源代码(Html 以及控制器).谁能帮我取回数据.

Parent.html

<html ng-app="parentWindowApp"><头><title>父窗口</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="description" content=""><meta name="author" content=""><script src="js/lib/angular-1.3.0/angular.js"></script><script src="js/lib/controllers/parentWindowController.js"></script><link href="js/lib/bootstrap/css/bootstrap.css" rel="样式表"><body ng-controller="parentWindowController"><div class="容器"><br><br><br><input type="text" ng-model="iptext.text" placeholder="I/P Value &AMP; 点击链接"/><a id="popupSymImg" tabindex="-1" ng-click="openChildWindow();">调用子窗口</a>

</html>

parentWindowController.js

var parentWindow = angular.module('parentWindowApp',[]);parentWindow.controller('parentWindowController', function ($scope,$window) {//$scope.shareData = "来自父窗口的父值";alert('控制器加载...');$scope.iptext = { 文本:''};$scope.openChildWindow = function(){//$window.childWindowValue = angular.toJson(value);$window.childWindowValue = angular.toJson($scope.iptext);alert("打开前:"+$window.childWindowValue);$window.open('ChildWindow.html', 300,200);//alert("打开后:"+$window.childWindowValue);};});

ChildWindow.html

 <html ng-app="childWindowApp"><头><title>子窗口</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="description" content=""><meta name="author" content=""><script src="${pageContext.request.contextPath}/js/lib/angular-1.3.0/angular.js"></script><script src="${pageContext.request.contextPath}/js/lib/controllers/childWindowController.js"></script><link href="${pageContext.request.contextPath}/js/lib/bootstrap/css/bootstrap.css" rel="stylesheet"><body ng-controller="childWindowController"><div class="容器"><表格><td><input ng-model='shareData.title' class="form-control" size="10" maxlength="10" placeholder="项目名称"需要><a id="popupSymImg" tabindex="-1" ng-click="closeChildWindow(shareData.title);">关闭子窗口 </a></td></tr>

</html>

childWindowController.js

var childWindow = angular.module('childWindowApp',[]);childWindow.controller('childWindowController', function ($scope,$window) {//$scope.shareData = "来自父窗口的父值";alert("来自父级的值:"+angular.toJson($window.opener.childWindowValue));$scope.parentValue = $window.opener.childWindowValue;$scope.shareDatas = [{title: '项目 1'},{title: '项目 2'},{title: '项目 3'},{title: '项目 4'},{title: '项目 5'},{title: '项目 6'},{title: '项目 7'}];$scope.closeChildWindow = 函数(值){//alert('In Opener :: '+$window.opener.childWindowValue);//alert('子窗口::'+$scope.parentValue);alert('点击值::'+值);$window.childWindowValue = 值;alert('当前在窗口属性中:'+$window.childWindowValue);//$window.close();};});

截图

解决方案

我已经使用 showModalDialog 在 Parent 和 Child 之间交换数据,它工作正常.

另请参阅showModalDialog 教程.

I have a scenario where I need to exchange the data dynamically (From PARENT To CHILD and Reversal (PARENT to CHILD) ). I referred from couple of links and it helped me to pass the data from PARENT to CHILD, however From CHILD to PARENT didn't.

Below i have the source code of Parent to Child (Html as well as the controller). Could somebody help me how to take the data back again.

Parent.html

<!DOCTYPE html>
<html ng-app="parentWindowApp">
    <head>
        <title>Parent Window</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <script src="js/lib/angular-1.3.0/angular.js"></script>
        <script src="js/lib/controllers/parentWindowController.js"></script>
        <link href="js/lib/bootstrap/css/bootstrap.css" rel="stylesheet">
    </head>
    <body ng-controller="parentWindowController">

        <div class="container">

            <br><br><br>
            <input type="text" ng-model="iptext.text" placeholder="I/P Value &AMP; Click Link"/>
            <a id="popupSymImg" tabindex="-1" ng-click="openChildWindow();"> Call Child Window </a>
        </div>
    </body>
</html>

parentWindowController.js

var parentWindow = angular.module('parentWindowApp',[]);

parentWindow.controller('parentWindowController', function ($scope,$window) {

   //$scope.shareData = "Parent Value from Parent Window";

   alert('Controller Loaded...');

   $scope.iptext = { text: ''};

   $scope.openChildWindow = function(){

       //$window.childWindowValue = angular.toJson(value);
       $window.childWindowValue =  angular.toJson($scope.iptext);
       alert("Before Open : "+$window.childWindowValue);

       $window.open('ChildWindow.html', 300,200);

       //alert("After Open  : "+$window.childWindowValue);

   };

 });

ChildWindow.html

 <!DOCTYPE html> <html ng-app="childWindowApp">
     <head>
         <title>Child Window</title>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <meta name="description" content="">
         <meta name="author" content="">
         <script src="${pageContext.request.contextPath}/js/lib/angular-1.3.0/angular.js"></script>
         <script src="${pageContext.request.contextPath}/js/lib/controllers/childWindowController.js"></script>
         <link href="${pageContext.request.contextPath}/js/lib/bootstrap/css/bootstrap.css" rel="stylesheet">
  </head>
  <body ng-controller="childWindowController">

         <div class="container">

             <table>
                 <tr ng-repeat='shareData in shareDatas'>
                     <td >
                         <input ng-model='shareData.title' class="form-control" size="10" maxlength="10" placeholder="Item Name"
 required> 
                         <a id="popupSymImg" tabindex="-1" ng-click="closeChildWindow(shareData.title);"> Close Child Window </a>
                     </td>    
                 </tr>
             </table>
         </div>
   </body> </html>

childWindowController.js

var childWindow = angular.module('childWindowApp',[]);


childWindow.controller('childWindowController', function ($scope,$window) {

   //$scope.shareData = "Parent Value from Parent Window";

   alert("Value from parent :"+angular.toJson($window.opener.childWindowValue));

   $scope.parentValue = $window.opener.childWindowValue;

     $scope.shareDatas = [
                        {title: 'Item 1'},
                        {title: 'Item 2'},
                        {title: 'Item 3'},
                        {title: 'Item 4'},
                        {title: 'Item 5'},
                        {title: 'Item 6'},
                        {title: 'Item 7'}
                    ];

   $scope.closeChildWindow = function(value){


       //alert('In Opener :: '+$window.opener.childWindowValue);

       //alert('Child Window :: '+$scope.parentValue);

       alert('Value on Click :: '+value);

       $window.childWindowValue = value;

       alert('Current in Window Properties : '+$window.childWindowValue);

       //$window.close();

   };  


 });

Screenshot

解决方案

I have used showModalDialog to exchange the data between Parent and Child and it is working fine.

See also showModalDialog tutorial.

这篇关于如何在 AngularJS 中将值从弹出窗口交换到父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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