Angularjs,传递路线之间的范围 [英] Angularjs, passing scope between routes

查看:102
本文介绍了Angularjs,传递路线之间的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那绵延几页表格的情况下(也许并不理想,但是这是怎么回事)。我想有该被填写为您去整个形成一个范围,所以,如果用户进入来回步骤之间很容易记住的状态。

I have a situation with a form that stretches over several pages (might not be ideal, but that is how it is). I'd like to have one scope for the entire form that gets filled in as you go along, so that if the user goes back and forth between steps it's easy to remember the state.

所以,我需要做的,非常-伪code:

So I need to do, in very-pseudo-code:

 1. Set $scope.val = <Some dynamic data> 
 2. Click a link and be routed to a new template (probably with the same controller). 
 3. $scope.val should still be the same value as it was on the last page.

在某种程度上为范围持续数据去了解这个正确的方式,或者是有一些其他的方式?你甚至可以创建具有路由之间的持久范围控制器,除为拯救它当然是一个数据库。

Is somehow persisting data for the scope the right way to go about this, or is there some other way? Can you even create a controller that has a persisted scope between routes, except for saving it in a database of course.

推荐答案

您需要使用一个服务,因为服务将在整个应用程序的生命存在。

You need to use a service since a service will persist throughout your app's life.

比方说你要保存有关用户的数据。

Lets say you want to save data pertaining to a user

这是你如何定义服务:

app.factory("user",function(){
        return {};
});

在你的第一个控制器:

app.controller( "RegistrationPage1Controller",function($scope,user){
    $scope.user = user;
    // and then set values on the object
    $scope.user.firstname = "John";
    $scope.user.secondname = "Smith";
});

app.controller( "RegistrationSecondPageController",function($scope,user){
        $scope.user = user;
        // and then set values on the object
        $scope.user.address = "1, Mars";

    });

这篇关于Angularjs,传递路线之间的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆