博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS] Default Child state and nav between child state
阅读量:6893 次
发布时间:2019-06-27

本文共 3018 字,大约阅读时间需要 10 分钟。

Let's say we want a parent state which is a abstract state. Two children states, one is for sinlge account view and another is for multi-accounts view. 

By default, we want to go single account view:

.state('selfcare.my-services', {                url: 'my-services',                abstract: true,                resolve: {                    authenticate: authenticate                }            })            .state('selfcare.my-services.single', {                url: '',                views: {                    'main@': {                        template: '
' } }, resolve: {
router: ($q, UserService) => { let loginName = UserService.userProfileDataFromJWT.loginName; return UserService.getServiceDetails(loginName) .then((res) => { if (_.size(res.billAccounts) > 1) { return $q.reject('TO_MULTI_SERVICES'); } else { return $q.when(); } }); } } })

The idea is:

  • Keep the default child state's url empty, so it knows this child state is default one, it will have the same url as parent.
  • Make parent state as abstract state.
  • authenticate is only need for parent, because it always goes from parent to children, so if parent is authenticated then it means child state is also authenticated.

 

Then in the code we have:router resolve block, you can name it anything you want:

router: ($q, UserService) => {                        let loginName = UserService.userProfileDataFromJWT.loginName;                        return UserService.getServiceDetails(loginName)                            .then((res) => {                                if (_.size(res.billAccounts) > 1) {                                    return $q.reject('TO_MULTI_SERVICES');                                } else {                                    return $q.when();                                }                            });

It says that if there is multi service accounts then go multi-service account. So reject the promise, it will be handled by $stateChangeError.

 

$stateChangeError:

$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {            switch (error) {
case 'TO_SINGLE_SERVICE': return $state.go('selfcare.my-services.single'); case 'TO_MULTI_SERVICES': return $state.go('selfcare.my-services.compsite'); default: $state.go('login.main'); } $('.progress-bar') .hide(); });

 

The same as to handle multi-account view. And because you cannot access abstract directly by url, so to access the account, you need to do:

ui-sref="selfcare.my-services.single"

 

转载地址:http://pjhbl.baihongyu.com/

你可能感兴趣的文章
项目、软件开发过程中版本术语
查看>>
T-SQL中INSERT、UPDATE
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>
Python实践之(七)逻辑回归(Logistic Regression)
查看>>
PAT (Advanced Level) 1107. Social Clusters (30)
查看>>
【开源社群系统研发日记五】ThinkSNS+ 是如何计算字符显示长度的
查看>>
Nodejs日志管理log4js
查看>>
python获取昨日日期
查看>>
海康威视 - 萤石云开放平台 js 版
查看>>
关于分销平台
查看>>
jquery实用的一些方法
查看>>