2016-03-17 130 views
1

如何將數據共享到所有控制器?在AngularJs中共享數據

我有要共享到其他控制器

sampleApp.controller('PhoneListCtrl', 
['$scope', '$http', 
function($scope, $http) { 
    $http.get('App_Data/phonelist.json'). 
     success(function(returnDataFrmJson){ 
      $scope.phonesScope = returnDataFrmJson; 
     }); 
}]); 

控制器將訪問的第一個

sampleApp.controller('AddIPhoneController', 
['$scope', '$http', 
function($scope, $http) { 
    $scope.newInput= 'sample text'; 

    $scope.sharedText= dataFromSharedControll; 
}]); 
共享數據,從服務器(file.json)拉數據的控制器

將顯示數據的html文件。

{{newInput}} {{sharedText}} 

回答

2

嗯,有你的各種選項,如使用: $sessionStoragelocalStorageappConstant$localStorage等。

您甚至可以使用FactoryServices共享控制器之間的數據。我會給你一個簡單的例子,使用$sessionStorage.

爲了使用$sessionStorage$localStorage,你需要注入ngStorage。

首先在您的索引中。HTML,包括源:

<script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>  
在你的模塊定義

然後,注入ngStorage:

var sampleApp = angular.module('Your App Name', ['ngStorage']); 

而且,在你controllers

sampleApp.controller('PhoneListCtrl', 
['$scope', '$http', '$sessionStorage', 
function($scope, $http,$sessionStorage) { 
    $http.get('App_Data/phonelist.json'). 
     success(function(returnDataFrmJson){ 
      $scope.phonesScope = returnDataFrmJson; 
      $sessionStorage.sharedValue = returnDataFrmJson;//keeping the value in session 
     }); 
}]);  

controller將訪問共享數據的第一個

sampleApp.controller('AddIPhoneController', 
['$scope', '$http','$sessionStorage', 
function($scope, $http,$sessionStorage) { 
    $scope.newInput= 'sample text'; 

    $scope.sharedText= $sessionStorage.sharedValue; 
}]); 

然後在您的View

{{newInput}}{{sharedText}} 
+0

這一個像魔術^^,謝謝,但我怎麼能得到確切的數據,因爲它在JSON格式。例如[{「name」:「oneName」,「address」:123},{「name」:「twoName」,「address」:341}] –

+0

您可以在視圖中以{{sharedText.name}} {{sharedText.address}}等等。 –

+0

明白了。按「。」綁定在視圖^^,你有這個服務和本地存儲比較哪個/哪個對那個有好處 –

0

如果我正確理解你的問題,你可以創建一個服務,並將它注入到其他控制器,像這樣:

sampleApp.service('sharedProperties', function() { 
     var sharedText = ''; 
     return { 
      getSharedText: function() { 
       return sharedText; 
      }, 
      setSharedText: function(newText) { 
       sharedText = newText; 
      } 
     } 
    } 

然後在你的控制器:

sampleApp.controller('ControllerName', ['sharedProperties', function(sharedPropertied) { 
     var text = sharedProperties.getSharedText(); 
    }]); 

顯然,你應該通過注入服務並將其設置在另一個控制器中來設置文本。

0

您應該通過服務共享數據。此外,控制器中的$ http呼叫應移至服務中。如果您然後在控制器中注入服務,您可以在控制器中調用該服務的方法/數據。

我建議你看看約翰爸爸的風格指南這裏找到:https://github.com/angular/angular

0

你做錯了,你想控制器之間共享數據服務應該存在不控制器

服務

sampleApp.service("MyService",function($http){ 
     this.phoneList = function(){ 
      return $http.get('App_Data/phonelist.json'); 
     };   
}); 

控制器1

sampleApp.controller('PhoneListCtrl1',function($scope,MyService) { 
     MyService.phoneList() 
      .then(function(list){ 
       $scope.phoneList = list; 
      }); 
    }); 

控制器2

sampleApp.controller('PhoneListCtrl2',function($scope,MyService) { 
      MyService.phoneList() 
       .then(function(list){ 
        $scope.phoneList = list; 
       }); 
    }); 
0

這取決於要求:

  1. 如果你的目的是分享控制器之間的數據,而不 通知控制器當數據被填充/更新,去 與服務/工廠方法。
  2. 如果你還想通知其他控制器,而填充/更新數據 那麼你可以使用角度爲 事件($ broadcast/$ emit)。
  3. 如果您想保留頁面刷新或瀏覽器 附近的數據,可以選擇會話/本地存儲。