Angular JS /数据处理/概述页面

时间:2013-07-01 20:06:57

标签: angularjs

我正在开发一个Angular-App。用户必须在不同页面上输入数据,而可以在页面之间切换。此外,还有一个“概述页面”,用户可以在其中查看输入的数据。

所以现在我正在思考如何在概述页面上显示输入的数据。我应该只使用$ rootScope来获取数据,还是将它存储在JSON对象中更好? - 但Angular不建议这样做 - 在服务中存储数据?

那么从哪个页面获取输入的数据呢?

谢谢!

1 个答案:

答案 0 :(得分:2)

如果您希望在单个页面中的实际HTML页面之间保留数据而不是routes,则可以使用LocalStorageHere is a service这会让事情变得更容易。

另一种方法是使用cookie。您可以在Angular here中阅读有关使用Cookie的更多信息。

如果您希望将数据保存在不同的routes中,则需要创建共享service。以下是此类方法的示例:

<div ng-app="myApp">
    <a href="#one">one</a> | <a href="#two">two</a>
    <div ng-view></div>
    <script id="one.html" type="text/ng-template"><div><h1>Template One</h1>foo = {{shared.foo}}</div></script>
    <script id="two.html" type="text/ng-template"><div><h1>Template Two</h1>foo = {{shared.foo}}</div></script>
</div>

angular.module('myApp', []).
config(function($routeProvider){
    $routeProvider.
    when('/one', {templateUrl: 'one.html', controller: 'OneCrtl'}).
    when('/two', {templateUrl: 'two.html', controller: 'TwoCrtl'}).
    otherwise({redirectTo: '/one'});
}).
service('sharedService', [function() {
  this.foo = 'bar';
}]).
controller('OneCrtl', function($scope, sharedService){
    $scope.shared = sharedService
}).
controller('TwoCrtl', function($scope, sharedService){
    $scope.shared = sharedService
});

Here's a fiddle

相关问题