angularjs 多重解析与加载序列和身份验证权限 [英] angularjs multiple resolve with loadsequence and auth permission

查看:17
本文介绍了angularjs 多重解析与加载序列和身份验证权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考这里的这个问题,我遇到了同样的问题.
我们如何设置多个解析,一个使用 currentAuth,另一个使用 loadsequence 加载控制器和脚本?

Referring to this question here, I am having the same problem.
How can we set up a multiple resolve, one with currentAuth and another with loadsequence which loads controllers and scripts?


这是我的状态配置:

.state('app.example', {
        url: "/example",
        templateUrl: "assets/views/example.html",
        resolve:{
            loadSequence: loadSequence('jquery-sparkline', 'exampleCtrl')
        },
        title: 'example',
        ncyBreadcrumb: {
            label: 'example'
        }
    })


这是我的 loadsequece 函数:

function loadSequence() {
    var _args = arguments;
    return {
        deps: ['$ocLazyLoad', '$q',
        function ($ocLL, $q) {
            var promise = $q.when(1);
            for (var i = 0, len = _args.length; i < len; i++) {
                promise = promiseThen(_args[i]);
            }
            return promise;

            function promiseThen(_arg) {
                if (typeof _arg == 'function')
                    return promise.then(_arg);
                else
                    return promise.then(function () {
                        var nowLoad = requiredData(_arg);
                        if (!nowLoad)
                            return $.error('Route resolve: Bad resource name [' + _arg + ']');
                        return $ocLL.load(nowLoad);
                    });
            }

            function requiredData(name) {
                if (jsRequires.modules)
                    for (var m in jsRequires.modules)
                        if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
                            return jsRequires.modules[m];
                return jsRequires.scripts && jsRequires.scripts[name];
            }
        }]
    };
}


这是我的 currentAuth 工厂:

currentAuth: ['Auth', function(Auth) {
                return Auth.$requireSignIn()
            }]

推荐答案

ui-router 文档 :

resolve 属性是一个地图对象.地图对象包含以下键/值对:

The resolve property is a map object. The map object contains key/value pairs of:

key – {string}:要注入控制器的依赖项的名称.

key – {string}: a name of a dependency to be injected into the controller.

工厂 - {string|function}:如果是字符串,则它是服务的别名.否则,如果是函数,则将其注入并将返回值视为依赖项.如果结果是一个promise,则在控制器实例化之前解决,并将其值注入控制器.

factory - {string|function}: If string, then it is an alias for a service. Otherwise if function, then it is injected and the return value is treated as the dependency. If the result is a promise, it is resolved before the controller is instantiated and its value is injected into the controller.

所以你可以在你的状态解析中配置你的状态添加功能:

so you can configure your state adding functions in your state resolve :

.state('app.example', {
    url: "/example",
    templateUrl: "assets/views/example.html",
     resolve: { 
scripts: loadSequence('jquery-sparkline', 'exampleCtrl').deps,
currentAuth: function(Auth){ return Auth.$requireSignIn();}
},
    title: 'example',
    ncyBreadcrumb: {
        label: 'example'
    }
})

这篇关于angularjs 多重解析与加载序列和身份验证权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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