2017-03-09 25 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

相關問題