I'm using Angular UI-Router with a resolve function, but when I minify the resolve function, my whole application breaks because the resolve function syntax is not correct for minification. It needs to be String-Injection based as outlined here. I'm just not sure how to write it. Any suggestions?
// Resolves
var checkAuthentication = function($q, $location, $rootScope, Users) {
if ($rootScope.user) return true;
if (!$rootScope.user) {
var deferred = $q.defer();
Users.get(null, function(user) {
if (!user) {
window.location = '/';
return false;
}
console.log('User fetched: ', user);
$rootScope.user = user;
deferred.resolve();
}, function() {
window.location = '/';
return false;
});
return deferred.promise;
}
};
// Routes
angular.module('Dashboard').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For any unmatched url, redirect to '/'
$urlRouterProvider.otherwise('/dashboard');
// Now set up the states
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/content/dashboard.html',
resolve: {
checkAuthentication: checkAuthentication
}
})
I'm using Angular UI-Router with a resolve function, but when I minify the resolve function, my whole application breaks because the resolve function syntax is not correct for minification. It needs to be String-Injection based as outlined here. I'm just not sure how to write it. Any suggestions?
// Resolves
var checkAuthentication = function($q, $location, $rootScope, Users) {
if ($rootScope.user) return true;
if (!$rootScope.user) {
var deferred = $q.defer();
Users.get(null, function(user) {
if (!user) {
window.location = '/';
return false;
}
console.log('User fetched: ', user);
$rootScope.user = user;
deferred.resolve();
}, function() {
window.location = '/';
return false;
});
return deferred.promise;
}
};
// Routes
angular.module('Dashboard').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For any unmatched url, redirect to '/'
$urlRouterProvider.otherwise('/dashboard');
// Now set up the states
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/content/dashboard.html',
resolve: {
checkAuthentication: checkAuthentication
}
})
Share
Improve this question
edited May 23, 2017 at 12:17
CommunityBot
11 silver badge
asked Jul 8, 2014 at 15:44
ac360ac360
7,83514 gold badges55 silver badges95 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 23The way you would normally do this is by passing an array to the resolve:
resolve: {
welcome: ['$q', function ($q) {
var def = $q.defer();
setTimeout(function () {
def.resolve("Hello World!");
},500);
return def.promise;
}]
}
With that in mind, you can define your var like this:
var welcome = ['$q', function ($q) {
var def = $q.defer();
setTimeout(function () {
def.resolve("Hello World!");
},500);
return def.promise;
}]
but that of course isn't really reuseable, so at that point I would suggest moving it to a service.
.module("Dashboard")
func - else it's missing the closing brackets and what npt – tymeJV Commented Jul 8, 2014 at 15:47var myfn = ['$q', function ($q) {...
If you want to be able to reuse it, it should probably be a service instead. – Kevin B Commented Jul 8, 2014 at 15:50