2017-03-09 9 views
0

2つの角度コントローラ間の通信を設定しようとしています(サービスはオプションではありません)。私は必死に失敗しています。ここ

が、私は両方$発すると$あなたが$localStorage$stateParamsまたは$cookies、あるいはを使用することができます

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'、関数(イベント、データ){//データで何かを実行});'。 – 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

をブロードキャストを使用してみました私のコードのいくつか... です...私は、一般的に値を送信するために$stateParamsを好みます状態とコントローラには反対します。

コントローラからの$stateParamsを使用してファイルを読み取りました。詳細はこちらhere

関連する問題