Tôi có bộ điều khiển A gửi một số dữ liệu đến dịch vụ chia sẻ và bộ điều khiển B để đọc dữ liệu đó. Vấn đề là Bộ điều khiển B nằm trên một trang khác (trên cùng một trang web) so với bộ điều khiển A, tức là tôi làm điều đó. trên bộ điều khiển A, nút bấm (gửi dữ liệu đến dịch vụ) và mở một trang khác có bộ điều khiển B để đọc dữ liệu từ dịch vụ. Nhưng không có gì xảy ra, do đó, câu hỏi của tôi là có thể điều khiển trên các trang khác nhau giao tiếp theo cách này?Giao tiếp Angularjs giữa các bộ điều khiển trong các tab khác nhau
Sau đây là cách tôi đã cố gắng để làm điều đó nhưng không có may mắn:
dịch vụ:
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;
});
phần có liên quan của bộ điều khiển 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);
};
phần có liên quan của bộ điều khiển 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);
});
});
Và thậm chí console.log("TEST");
không bị sa thải.
Thêm một FYI hơn một giải pháp, nhưng nếu bạn đặt 'controllerCommunicationService.setDataForBroadcast()' trong khoảng thời gian chờ $ với độ trễ đủ, mã của bạn sẽ hoạt động được cung cấp trang đích có đủ thời gian để tải. Chắc chắn không đáng tin cậy mặc dù. – Lavamantis