<分区>
我有 Controller A,它发送一些数据到共享服务和 Controller B,它应该读取该数据。问题是 Controller B 与 Controller A 位于不同的页面(在同一网站上),即我做了 smth。在 Controller A 上,单击按钮(将数据发送到服务)并打开另一个页面,其中 Controller B 应从服务中读取数据。但是没有任何反应,所以我的问题是不同页面上的 Controller 可以通过这种方式通信吗?
这是我尝试过但没有成功的方法:
服务:
publicApp.angularModule.service('controllerCommunicationService', function ($rootScope) {
var communicationService = {};
communicationService.data = null;
communicationService.setDataForBroadcast = function(data) {
this.data = data;
this.broadcastData();
};
communicationService.broadcastData = function() {
$rootScope.$broadcast('handleBroadcast');
};
return communicationService;
});
Controller A的相关部分:
publicApp.angularModule.controller('PublicRoutinesCtrl', function ($scope, $rootScope, routinesService, controllerCommunicationService, bootstrapCarouselService) {
$scope.goToNextScreen = function() {
var currentIndex = bootstrapCarouselService.getcurrentSlideIndex();
controllerCommunicationService.setDataForBroadcast($scope.items[currentIndex].Routine.RoutineId);
};
controller B的相关部分:
$rootScope.$on('handleBroadcast', function () {
console.log("TEST");
$http.post("/EditWorkout/PostRoutineId", { routineId: controllerCommunicationService.data })
.success(function() {
console.log("success");
})
.error(function (responseData) {
console.log("Error !" + responseData);
});
});
甚至 console.log("TEST");
也不会被触发。