angularjs使用$on和$broadcast在主控制器与另一个控制器之间进行通信

angularjs使用$on和$broadcast在主控制器与另一个控制器之间进行通信,angularjs,asp.net-mvc,master-pages,Angularjs,Asp.net Mvc,Master Pages,我正在尝试设置两个角度控制器之间的通信(服务不是选项)。我正在绝望地失败 这是我的一些代码。。。 我尝试同时使用$emit和$broadcast invoiceApp.controller('masterReportConrtoller', ['$scope', '$location', 'authService', 'usSpinnerService', 'dateService', 'settingsService','$rootScope', function ($scope, $loca

我正在尝试设置两个角度控制器之间的通信(服务不是选项)。我正在绝望地失败

这是我的一些代码。。。 我尝试同时使用$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');}
invoiceApp.controller('masterReportConrtoller',['$scope','$location','authService','usSpinnerService','dateService','settingsService','$rootScope',
函数($scope、$location、authService、usSpinnerService、dateService、setingsService、$rootScope)
////用户是否有效
////
//$rootScope.$on(“主报告控制器”,函数(){
//$scope.parentmethod();
//    });
//$scope.parentmethod=函数(){
//    //
$scope.masterReportConrtoller.getUserDetails=函数(){
调试器;
settingsService.getUserDetails().then(函数(响应){
var Loginda={
用户名:response.d.user.Email,
密码:response.d.user.UserPassword
};
authService.login(loginda).then(函数(响应){
调试器;
$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=新日期();

如果(fullDate您可以使用
$localStorage
$stateParams
$cookies
甚至…我通常更喜欢
$stateParams
将值和对象发送到状态和控制器

$state.go('state2', { someParam : 'broken magic' });

使用控制器中的
$stateParams
读取文件。可以找到详细信息

根据您的父子控制器关系,您可以在代码中使用
$scope.$broadcast
$scope.$on

试着这样做:

//masterReportConrtoller
$scope.$broadcast("myCustomEvent", { isValidUser: false });

//InvoiceController
$scope.$on("myCustomEvent" , function(event, data){
   //do something with data
});
请注意,如果
masterreportconrtoler
是父控制器,而
InvoiceController
是子控制器,则这将起作用。如果不是这样,则使用
$rootScope.$broadcast
$rootScope.$on


您可以找到更多详细信息。

您应该有一个回调函数来处理事件的发出。
$rootScope.$on('masterReportConrtoller',function(event,data){//do something with data})
。我可以使用页面加载事件吗?
事件
在本例中是使用
$rootScope.$broadcast('masterreportconrtoler',false);
触发的实际事件。因此,如果我没有事件,您有什么建议?您可以轻松地使用
$rootScope.broadcast触发您自己的自定义事件(“myCustomEvent”{something:“将要发送的数据”});
然后,使用
$rootScope.$on('myCustomEvent',函数(事件,数据){//do something with data})捕获该事件;
。但是,如果您只需要此父子控制器通信,则应避免使用
$rootScope
。此链接中已解释了所有内容:。