2017-03-09 40 views
0

我想设置两个角度控制器之间的通信(服务不是一个选项)。我拼命地失败了。

这里是我的一些代码... 我试图同时使用$发射和$广播

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');} 
+0

你应该有一个回调函数来处理事件发生的时间。 '$ rootScope。$ on('masterReportConrtoller',function(event,data){//用数据做某事});'。 – eminlala

+0

我可以使用页面加载事件吗? –

+0

'event'在这种情况下是使用$ rootScope。$ broadcast('masterReportConrtoller',false)''触发的实际事件。 – eminlala

回答

1

根据您的父母 - 子女控制人关系,您可以在代码中使用$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

0

您可以使用$localStorage$stateParams$cookies甚至......我一般喜欢$stateParams发送值并反对国家和控制器。

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

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

相关问题