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"
  • 相关阅读:
    内存泄漏 Memory Leaks 内存优化 MD
    Handler Thread 内部类引起内存泄露分析
    为什么不取消注册BroadcastReceiver会导致内存泄漏
    WebChromeClient 简介 API 案例
    WebViewClient 简介 API 案例
    java.net.URI 简介 文档 API
    android.net.Uri 简介 API
    RV 多样式 MultiType 聊天界面 消息类型 MD
    JS函数声明与定义,作用域,函数声明与表达式的区别
    CSS中table tr:nth-child(even)改变tr背景颜色: IE7,8无效
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5542949.html
Copyright © 2011-2022 走看看