2017-02-11 1 views
0

定数の値をルートコントローラから変更しようとしています。 それから、状態はログインコントローラに送られ、ここで定数の値はまだ古い値です。定数値を変更し、角度1.4.9のサブコントローラにブロードキャストします。

当初定数は、次のように設定されている:

var myApp = angular.module("app"); 
myApp.constant("clientsList", [{"object1":....}]); 

私はルートコントローラで

$rootScope.$emit('updateClients', null); 
$state.go('login', {}, {reload: true}); 

が含まれているログアウト機能があります後、

> $rootScope.$on('updateClients', function(event, clients) { 
>   _this.clientsList = clients; 
>   angular.module("app").constant("clientsList", clients); 
>  }); 

、ログインコントローラでのstate.go( 'login')によってリダイレクトされています:

.controller('LoginController', LoginController); 
function LoginController(clientsList) { 
    // clientsList still have the old value here: 
} 

clientsList定数の値を変更するにはどうすればよいですか?

+0

「定数の変更」こと:私たちは、クライアントと店をフェッチするために同じ工場を使用し、このダミーの例を作成し、その後、我々は二つの異なるコントローラにclientsListを取得

あなたはそれをしてはいけないと言ってもいいでしょう:p – Canastro

+0

私はこれについて考えています。しかし、ログインしたユーザーに関連するデータを含むプロバイダです。私はそうでなければサービスを使用することができます... –

+0

あなたは、グローバルにアクセスしたいデータを格納するために、常にプロバイダ(サービスまたは工場)を使用します。 – Canastro

答えて

1

APIへの呼び出しを実行して結果を保存するには、ファクトリ(または必要に応じてサービス)を使用することをお勧めします。これにより、すべてのコントローラでこれらの値にアクセスできます。

angular.module('webapp', []) 
 
     .controller('AppCtrl', function($scope, DummyFactory) { 
 
      $scope.clientsList = []; 
 
      $scope.getClients = function() { 
 
      DummyFactory.getClients().then(function(clientsList) { 
 
       $scope.clientsList = clientsList; 
 
      }); 
 
      }; 
 
     
 
     }) 
 
     .controller('OtherCtrl', function($scope, DummyFactory) { 
 
      $scope.clientsList = DummyFactory.clientsList; 
 
     }) 
 
     .factory('DummyFactory', function ($q, $timeout) { 
 
     var clientsList = []; 
 
     
 
     var getClients = function() { 
 
      // Here replace with your service call 
 
      return $q(function (resolve) { 
 
       $timeout(function() { 
 
       var result = [{name: 'a'}, {name:'b'}]; 
 
       
 
       // Here I use this so I don't create a new array 
 
       // this way the reference is not lost in "Other Controller" 
 
       // You could assign a new array, but then you 
 
       // would have to have a $watch on the "OtherController" 
 
       Array.prototype.push.apply(clientsList, result); 
 
       resolve(clientsList); 
 
       }, 500); 
 
      }); 
 
     }; 
 
     
 
     return { 
 
      clientsList: clientsList, 
 
      getClients: getClients  
 
     }; 
 
     });
<!DOCTYPE html> 
 
    <html ng-app="webapp"> 
 
    <head> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script> 
 
    </head> 
 
    <body ng-controller="AppCtrl"> 
 
     <div ng-controller="AppCtrl"> 
 
      <h1>App Controller:</h1> 
 
      <button ng-click="getClients()">GET CLIENTS</button> 
 
      <ul ng-repeat="item in clientsList"> 
 
       <li ng-bind="item.name"></li> 
 
      </ul> 
 
     </div> 
 
     
 
     <div ng-controller="OtherCtrl"> 
 
      <h1>Other Controller:</h1> 
 
      <ul ng-repeat="item in clientsList"> 
 
       <li ng-bind="item.name"></li> 
 
      </ul> 
 
     </div> 
 
    </body> 
 
    </html>

関連する問題