Reputation: 1083
I have a template and a controller. There is some code in controller executed whenever the state is changed to the respective view. How can I make sure none of the components of template gets rendered until the controller completes its execution. I already know about resolve - promise as answered in this question -
Defer template loading when using ng-controller
What I am looking for is a way to defer template rendering until my controller does it's job. I know about ng-cloak as well but it din't work when I applied it on the root element of my template. I may have a wrong understanding of it.
Here is the code -
<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>-->
And the controller -
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();
});
});
Upvotes: 0
Views: 38