我有一个页面,我在其中单击按钮并打开一个弹出页面。我需要一种方法将 json 数据从我的父页面传递到弹出窗口。这两个是独立的 Angular 应用程序。
然后数据会根据用户的操作进行修改并提交回父页面。我看过AngularJS: open a new browser window, yet still retain scope and controller, and services它将数据附加到父页面 Controller 中的 $window 对象,同时打开弹出窗口,将数据分配给弹出窗口的范围,然后将弹出窗口中修改的数据分配给弹出窗口 Controller 中的 $window.opener 对象,以便它可以在父级。
我想知道是否有一种 Angular 方法可以通过使用服务并在两个页面/应用程序之间传递数据来实现此目的。
我看过http://answer.techwikihow.com/27863/share-single-service-between-multiple-angular-js-apps.html它展示了如何在两个 ng-app 之间传递数据,但我想知道是否有比这更干净、直接的解决方案。我不想在弹出窗口的 Controller 中引用 $window 对象来访问共享数据,然后设置 opener 属性。我希望能够编写一个共享服务,它可能/可能不使用 $window 对象(最好不)并在父级和弹出窗口之间传递数据。
最佳答案
我最终做了以下事情:
在我的父页面 Controller 中我做了:
function ParentCtrl($scope, window){ window.parentScope = $scope; window.open(childPageURL, params); }
在我的子 Controller 中:
function ChildCtrl($scope, window){ var sharedData = window.opener.parentScope.someData; //Some code which changes sharedData window.opener.parentScope.someData = sharedData; window.opener.parentScope.$apply(); }
这似乎对我有用。我唯一想要的就是抽象出服务中的逻辑,两个 Controller 都可以与之通信并共享数据。
关于javascript - 在 angularjs 中在父页面和弹出页面之间传递数据的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018006/