最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular UI-Router Minification Error - How Can I Change Resolve Syntax To Be String Injection Based? - Stack Overfl

programmeradmin4浏览0评论

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
  • Are you missing the rest of your .module("Dashboard") func - else it's missing the closing brackets and what npt – tymeJV Commented Jul 8, 2014 at 15:47
  • 1 Wouldn't you pass it in the same way you are with the .config? as an array? – Kevin B Commented Jul 8, 2014 at 15:48
  • tymeJV - I just put the .config function in for reference, so that's not the issue. The issue is only with the checkAuthentication function's syntax. – ac360 Commented Jul 8, 2014 at 15:49
  • Kevin, yes the config syntax is correct, but I'm still fairly new to angular and I don't think I would put the checkAuthentication function in an angular '.config' statement. – ac360 Commented Jul 8, 2014 at 15:50
  • 1 you don't have to. var 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
 |  Show 1 more comment

1 Answer 1

Reset to default 23

The 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.

发布评论

评论列表(0)

  1. 暂无评论