嗯,有多种选项供您使用:$sessionStorage
, localStorage
, appConstant
, $localStorage
等等。
您甚至可以使用以下方式在控制器之间共享数据Factory
and Services
。我将为您提供一个简单的使用示例$sessionStorage.
为了使用$sessionStorage
or $localStorage
,你需要注入ngStorage。
首先在你的index.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}}