angularjs 使用 $on 和 $broadcast 在主控制器与另一个控制器之间进行通信
angularjs using $on and $broadcast to communicate between a master controller to another controller
我正在尝试设置两个 angular 控制器之间的通信(服务不是一个选项)。我正在拼命失败。
这是我的一些代码...
我尝试同时使用 $emit 和 $broadcast
invoiceApp.controller('masterReportConrtoller', ['$scope', '$location', 'authService', 'usSpinnerService', 'dateService', 'settingsService','$rootScope',
function ($scope, $location, authService, usSpinnerService, dateService, settingsService, $rootScope )
////Is User Valid
////
//$rootScope.$on("masterReportConrtoller", function () {
// $scope.parentmethod();
// });
//$scope.parentmethod = function () {
// //
$scope.masterReportConrtoller.getUserDetails = function () {
debugger;
settingsService.getUserDetails().then(function (response) {
var loginData = {
userName: response.d.user.Email,
password: response.d.user.UserPassword
};
authService.login(loginData).then(function (response) {
debugger;
$scope.Limit = response.d.organization.Limit;
});
$scope.Limit = response.d.organization.Limit;
$scope.DocumentUsage = response.d.organization.DocumentUsage;
$scope.ExpirationDate = $scope.DateConvertfromJson(response.d.organization.ExpirationDate);
var fullDate = new Date();
if (fullDate <= $scope.ExpirationDate) {
$scope.ISvalidUser = false;
$rootScope.$broadcast('masterReportConrtoller', false);
}
else {
$rootScope.$broadcast('masterReportConrtoller', true);
}
});
}
}]);
invoiceApp.controller('InvoiceController', ['$scope', '$location', '$cookieStore', 'documentService', 'dialogs', 'usSpinnerService', 'settingsService', 'associatedEmailsService', '$rootScope',
function ($scope, $location, $cookieStore, documentService, dialogs, usSpinnerService, settingsService, associatedEmailsService, $rootScope) {
$rootScope.$on('masterReportConrtoller');}
您可以使用 $localStorage
、 $stateParams
或 $cookies
甚至...我通常更喜欢 $stateParams
将值和对象发送到状态和控制器。
$state.go('state2', { someParam : 'broken magic' });
使用 $stateParams
从控制器读取文件。详情可见here
根据您的父子控制器关系,您可以在代码中使用 $scope.$broadcast
和 $scope.$on
。
尝试这样的事情:
//masterReportConrtoller
$scope.$broadcast("myCustomEvent", { isValidUser: false });
//InvoiceController
$scope.$on("myCustomEvent" , function(event, data){
//do something with data
});
请注意,如果 masterReportConrtoller
是父控制器并且 InvoiceController
是子控制器,这将起作用。如果不是这种情况,则使用 $rootScope.$broadcast
和 $rootScope.$on
.
您可以找到更多详细信息 here。
我正在尝试设置两个 angular 控制器之间的通信(服务不是一个选项)。我正在拼命失败。
这是我的一些代码... 我尝试同时使用 $emit 和 $broadcast
invoiceApp.controller('masterReportConrtoller', ['$scope', '$location', 'authService', 'usSpinnerService', 'dateService', 'settingsService','$rootScope',
function ($scope, $location, authService, usSpinnerService, dateService, settingsService, $rootScope )
////Is User Valid
////
//$rootScope.$on("masterReportConrtoller", function () {
// $scope.parentmethod();
// });
//$scope.parentmethod = function () {
// //
$scope.masterReportConrtoller.getUserDetails = function () {
debugger;
settingsService.getUserDetails().then(function (response) {
var loginData = {
userName: response.d.user.Email,
password: response.d.user.UserPassword
};
authService.login(loginData).then(function (response) {
debugger;
$scope.Limit = response.d.organization.Limit;
});
$scope.Limit = response.d.organization.Limit;
$scope.DocumentUsage = response.d.organization.DocumentUsage;
$scope.ExpirationDate = $scope.DateConvertfromJson(response.d.organization.ExpirationDate);
var fullDate = new Date();
if (fullDate <= $scope.ExpirationDate) {
$scope.ISvalidUser = false;
$rootScope.$broadcast('masterReportConrtoller', false);
}
else {
$rootScope.$broadcast('masterReportConrtoller', true);
}
});
}
}]);
invoiceApp.controller('InvoiceController', ['$scope', '$location', '$cookieStore', 'documentService', 'dialogs', 'usSpinnerService', 'settingsService', 'associatedEmailsService', '$rootScope',
function ($scope, $location, $cookieStore, documentService, dialogs, usSpinnerService, settingsService, associatedEmailsService, $rootScope) {
$rootScope.$on('masterReportConrtoller');}
您可以使用 $localStorage
、 $stateParams
或 $cookies
甚至...我通常更喜欢 $stateParams
将值和对象发送到状态和控制器。
$state.go('state2', { someParam : 'broken magic' });
使用 $stateParams
从控制器读取文件。详情可见here
根据您的父子控制器关系,您可以在代码中使用 $scope.$broadcast
和 $scope.$on
。
尝试这样的事情:
//masterReportConrtoller
$scope.$broadcast("myCustomEvent", { isValidUser: false });
//InvoiceController
$scope.$on("myCustomEvent" , function(event, data){
//do something with data
});
请注意,如果 masterReportConrtoller
是父控制器并且 InvoiceController
是子控制器,这将起作用。如果不是这种情况,则使用 $rootScope.$broadcast
和 $rootScope.$on
.
您可以找到更多详细信息 here。