使用异步功能更改angularjs templateUrl

时间:2020-09-18 10:42:59

标签: javascript angularjs promise angular-ui-router webcrypto-api

需要在templateUrl中添加加密的动态路径变量,以便使用异步的Web-Crypto加密并返回Promise,因为我尝试从resolve调用Promise函数,因为它为此提供了support。但是无法从templateUrl块更新resolve

$routeProvider.when('/portal/page/get', {
    templateUrl: '/portal/long/path/page/get', // default path
    /*templateUrl: function() {
        const response = getSecretData();
        // obviously this is not going to work as function is async
        return '/portal/long/path/page/' + response + '/get';
    }*/
    resolve: {
        urlUpdater: function($route) {
            getSecretData().then(response => {
                // As this is async function this line executes after setting templateUrl
                // So its not updating with new urlTemplate
                $route.current.templateUrl = '/portal/long/path/page/' + response + '/get';
            });
        }
    }
});

使用Web-Crypto获取加密值的基本方法,

async function getSecretData() {
    let sKey;
    let sIV = window.crypto.getRandomValues(new Uint8Array(12));
    let output;
    await window.crypto.subtle.generateKey({
        name: "AES-GCM",
        length: 256,
    }, true, ["encrypt"]).then(function(key) {
        sKey = key;
    });

    await crypto.subtle.encrypt({
        name: "AES-GCM",
        iv: sIV,
        tagLength: 128
    }, heaowInsightsAPISecretKey, "nonSecretValue")).then(function (cipherText) {
        output = btoa(String.fromCharCode.apply(null, new Uint8Array(cipherText)));
    });
    return output;
}

$route.reload()中使用resolve将无济于事,因为它将变得递归。

那么如何通过异步功能设置/更新templateUrl?

0 个答案:

没有答案