我处于必须将用户定义的对象从一个控制器传递到另一个控制器的情况下,该控制器在新窗口中打开。为此,我尝试了几种常用方法,例如$emit
使用工厂或服务 $rootScope 并$broadcast
从那里到第二个控制器。
但我总是卡在 rootscope 中。我可以在那里记录变量,但是当新窗口打开时它们似乎消失了(新窗口的控制台中甚至没有出现未定义的变量)。我可以从 rootscope 获取固定变量,但第一个控制器没有发出任何内容。
有人知道我该如何解决这个问题吗?
这是我的代码:
// SHARING DATA BETWEEN CONTROLLERS VIA ROOT SCOPE:
var table_app = angular.module("tableApp", [])
.factory("sharedVars", function($rootScope, $log) {
/*
Receive emitted message and broadcast it.
*/
$rootScope.$on("handleEmit", function(event, object){
var value1 = object.data1;
var value2 = object.data2;
var value3 = object.data3;
$log.log(value2, value3) // WORKS
$rootScope.$broadcast("handleBroadcast",
{data1: value1, data2: value2, data3: value3});
})
// JUST TO CHECK IF I CAN GET DATA FROM HERE:
return { fixedSharedObject: {data1: 22, data2: 33, data3: 44}} // WORKS
})
.controller("FirstCtrl", function ($scope, $rootScope, $q, $log, sharedVars) {
$scope.onClick = function(number, adress) {
newWindow = window.open(adress, "Zweitfenster", "width=700, height=900");
newWindow.focus();
var lineNumber = document.getElementById("lineCell" + number).innerHTML;
var blockNumber = document.getElementById("blockCell" + number).innerHTML;
var tripId = document.getElementById("tripCell" + number).innerHTML;
$log.log(lineNumber, blockNumber, tripId); // WORKS
$scope.$emit("handleEmit",
{data1:lineNumber, data2: blockNumber, data3: tripId});
}
})
.controller("SecondCtrl", function($scope, $rootScope, $log, sharedVars){
$scope.singleTripLineNumber = sharedVars.fixedSharedObject; // WORKS
$scope.singleTripBlockNumber = sharedVars.fixedSharedObject;
$scope.$on("handleBroadcast", function(event, object){
var value1 = object.data1;
var value2 = object.data2; // DOES NOT WORK!
var value3 = object.data3;
$log.log(value3)
})
});