Javascript Can';不在控制器之间传递数据?
我不知道我做错了什么,我正在尝试全局存储数据,并通过服务将数据从一个控制器传递到另一个控制器。我将数据存储在一个控制器中,并确认它是在我的Javascript Can';不在控制器之间传递数据?,javascript,angularjs,controller,Javascript,Angularjs,Controller,我不知道我做错了什么,我正在尝试全局存储数据,并通过服务将数据从一个控制器传递到另一个控制器。我将数据存储在一个控制器中,并确认它是在我的buildReportController开始时存储的。然后,当我单击UI上的一个按钮时,它会打开reportResultsController。然而,问题是,我可以通过locationHistoryService.store()将数据正确存储在buildReportController中,但当我转到reportResultsController并调用loca
buildReportController
开始时存储的。然后,当我单击UI上的一个按钮时,它会打开reportResultsController
。然而,问题是,我可以通过locationHistoryService.store()
将数据正确存储在buildReportController
中,但当我转到reportResultsController并调用
locationHistoryService.get()时,
locationHistoryService`中的先前位置变量为空,就好像从未设置过数据一样。关于如何或如何“全局”存储数据并在控制器之间传递数据,有什么想法吗?下面是我的尝试。谢谢
在reportView.js中
angular.module('reportView', [])
.service('locationHistoryService', function(){
var previousLocation = "";
return {
store: function(location){
previousLocation = location;
},
get: function(){
return previousLocation;
}
};
});
angular.module('reportView', [])
.service('locationHistoryService',['$rootScope'] ,function($rootScope){
var previousLocation = "";
return {
store: function(location){
previousLocation = location;
$rootScope.$broadcast('previous-location-set');
},
get: function(){
return previousLocation;
}
};
});
在buildReport.js中
angular.module('buildReport', ['reportView'])
.controller('buildReportController', ['$rootScope', 'locationHistoryService'], function($rootScope, locationHistoryService){
$rootScope.$on('$locationChangeSuccess', function(e, newLocation, oldLocation){
locationHistoryService.store(oldLocation);
console.log("Old location: ", oldLocation);
});
}
在reportResults.js中
angular.module('reportResults', ['reportView'])
.controller('reportResultsController', ['$rootScope', 'locationHistoryService'], function($rootScope, locationHistoryService){
console.log("location : ", locationHistoryService.get());
}
angular.module('reportResults', ['reportView'])
.controller('reportResultsController', ['$rootScope', 'locationHistoryService'], function($rootScope, locationHistoryService){
$rootScope.$on('previous-location-set', function(){
console.log("location : ", locationHistoryService.get());
});
}
reportResults.js中的locationHistoryService.get()
方法在buildReport.js中设置之前被调用
最好是在设置了previousLocation变量后通知
在reportView.js中
angular.module('reportView', [])
.service('locationHistoryService', function(){
var previousLocation = "";
return {
store: function(location){
previousLocation = location;
},
get: function(){
return previousLocation;
}
};
});
angular.module('reportView', [])
.service('locationHistoryService',['$rootScope'] ,function($rootScope){
var previousLocation = "";
return {
store: function(location){
previousLocation = location;
$rootScope.$broadcast('previous-location-set');
},
get: function(){
return previousLocation;
}
};
});
在reportResults.js中
angular.module('reportResults', ['reportView'])
.controller('reportResultsController', ['$rootScope', 'locationHistoryService'], function($rootScope, locationHistoryService){
console.log("location : ", locationHistoryService.get());
}
angular.module('reportResults', ['reportView'])
.controller('reportResultsController', ['$rootScope', 'locationHistoryService'], function($rootScope, locationHistoryService){
$rootScope.$on('previous-location-set', function(){
console.log("location : ", locationHistoryService.get());
});
}
您的webapp应该只有一个由angular自动引导的模块,其他模块作为依赖项。写入服务的语法不正确。您编写了.service
,但返回了.factory
应该返回的对象。下面是代码的工作示例
此外,您编写的控制器缩小语法的安全性不正确,控制器的功能块应该是数组中的最后一项 您是否可以尝试更改服务的函数名,例如使用setPrevLocation
而不是get
使用getprevLocation
get
可能是一个关键字或某个reserver单词,但不确定。您试图通过factory更改服务?嗯,问题是,我首先转到html页面,该页面将ng controller
设置为buildReportController
,并将其设置在那里。然后,在UI上,我有一个按钮将我引导到reportResult
s html页面,该页面将ng controller
设置为reportresultcontroller
,但我想我错了。关于您的解决方案,有一个问题是:$broadcast
是否用于激发父->子?我不想改用$emit
吗?对于您的解决方案,我什么时候存储(
以前的位置
?$broadcast
和$emit
在谈论本地$scopes
时是有意义的,因为$rootScope
对于所有控制器和服务都是相同的,您可以使用它们中的任何一个。你可以store()
上一个位置,就像上次一样(在buildReport.js中)这对我不起作用不幸的是,从未调用$on