延迟模板渲染,直到控制器执行完成

时间:2015-04-20 07:22:35

标签: javascript jquery angularjs templates ngcloak

我有一个模板和一个控制器。只要状态更改为相应视图,控制器中就会执行一些代码。在控制器完成执行之前,如何确保模板的任何组件都不会呈现。我已经知道了解决 - 在这个问题中回答的承诺 -

Defer template loading when using ng-controller

我正在寻找的是一种推迟模板渲染的方法,直到我的控制器完成它的工作。我也知道ng-cloak,但是当我在模板的根元素上应用它时它不起作用。我可能对它有错误的理解。

这是代码 -

<div class="container">
    <div class="row">
        <div class="col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 text-center animated fadeInDown">
            <h3>Welcome to ***********</h3>

            <p>No authentication found. This may be because you are logged out.
            </p>

            <p>Please click the following button to authenticate you using portal.</p>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 text-center animated fadeInDown">
            <br/>
            <a class="btn btn-primary" target="_self"
               href="/portal/oauth/authorize?client_id=cms-web-view&response_type=token&redirect_uri={{returnToUri}}">Authenticate
            </a>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 text-center animated fadeInDown">
            <br/>
            <p class="m-t">
                <small> © 2015</small>
            </p>
        </div>
    </div>
</div>-->

控制器 -

define(['layout/module'], function (module) {

    'use strict';

    module.registerController('AuthCtrl', function($scope, $location, $state, $log, $http, $window) {

        $scope.returnToUri = $location.absUrl();

        var hash = $location.hash();
        $log.info($location.absUrl());
        if(hash.indexOf('access_token') > -1) {
            var extractRegex = /access_token=([a-zA-Z0-9-]*)&/;
            var result = extractRegex.exec(hash);
            if(result.length > 1) {
                $log.debug('Found access token in hash, redirecting to the app.', result[1]);
                localStorage.setItem('oauthToken', result[1]);
                $http.defaults.headers.common.Authorization = 'Bearer ' + result[1];
                $state.go('app.dashboard');
            } else {
                $log.error('access_token not extractable via regex from hash', hash);
            }
        } else if(hash.length > 0) {
            $log.debug('Hash didn\'t contain the access_token', hash);
        }

        // Circular redirection - An intermediate page is necessary
        //$window.location.href = '/portal/oauth/authorize?client_id=cms-web-view&response_type=token&redirect_uri=' + $location.absUrl();
    });
});

0 个答案:

没有答案