zoukankan      html  css  js  c++  java
  • [AngularJS] Default Child state and nav between child state

    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: '<clm-my-services></clm-my-services>'
                        }
                    },
                    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"
  • 相关阅读:
    当程序员的那些狗日日子
    程序员常犯的几个错误
    我没有帮你的义务,却有拒绝你的权力
    并发 并行 同步 异步 多线程的区别
    cookie的存取
    sqlserver 处理百万级以上的数据处理与优化
    为什么 jmeter 分布式测试,一定要设置 java.rmi.server.hostname
    详细解析 nginx uri 如何匹配 location 规则
    mysql innodb 从 ibd 文件恢复表数据
    mysql 从 frm 文件恢复 table 表结构的3种方法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5542949.html
Copyright © 2011-2022 走看看