如何將數據共享到所有控制器?在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}}
這一個像魔術^^,謝謝,但我怎麼能得到確切的數據,因爲它在JSON格式。例如[{「name」:「oneName」,「address」:123},{「name」:「twoName」,「address」:341}] –
您可以在視圖中以{{sharedText.name}} {{sharedText.address}}等等。 –
明白了。按「。」綁定在視圖^^,你有這個服務和本地存儲比較哪個/哪個對那個有好處 –