使用 Angular UI-Router 保留状态 [英] Preserve state with Angular UI-Router

查看:27
本文介绍了使用 Angular UI-Router 保留状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ng-view 的应用程序,可以向从联系人列表中选择的联系人发送电子邮件.当用户选择收件人"时,它会显示另一个视图/页面,他可以在其中搜索、过滤等.发送电子邮件"和联系人列表"是加载在 ng-view 中的不同 html 部分.

I have an app with a ng-view that sends emails to contact selected from a contact list. When the users select "Recipient" it shows another view/page where he can search, filter, etc. "Send email" and "Contact list" are different html partials that are loaded in the ng-view.

我需要保持发送表单状态,以便当用户从联系人列表中选择某人时,它会返回到相同的点(和相同的状态).我阅读了不同的解决方案($rootScope,使用 ng-show 隐藏 div,...)但我想知道 UI-router 是否会帮助我处理它的状态管理器.如果没有,是否还有其他现成的解决方案?

I need to keep the send form state so when the users select someone from the Contact List it returns to the same point (and same state). I read about different solutions ($rootScope, hidden divs using ng-show, ...) but I want to know if UI-router will help me with it's State Manager. If not, are there other ready-to-use solutions?

谢谢!

推荐答案

我采用的解决方案是使用服务作为我的数据/模型存储.它们在控制器更改时持续存在.

The solution i have gone with is using services as my data/model storage. they persist across controller changes.

示例

用户服务(我们的模型在控制器更改时保持不变)

the user service ( our model that persists across controller changes )

app.factory('userModel', [function () {
    return {
        model: {
            name: '',
            email: ''
        }
    };
}]);

在控制器中使用它

function userCtrl($scope, userModel) {
    $scope.user = userModel;
}

这样做的另一个优点是您可以轻松地在其他控制器中重用您的模型.

the other advantage of this is that you can reuse your model in other controllers just as easly.

这篇关于使用 Angular UI-Router 保留状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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