angularjsangular-ui-routerangularjs-serviceangularjs-factoryangular-ui-router-extras

Factory Methods Blank When Using In Resolve


I am trying to utilise a factory within my ui-router resolve. I can see my factory name is returned, however the methods do not appeart to be there:

router.js

define(['module', 'require'], function(module, require) {
    'use strict';

    var Router = function ($stateProvider, $urlRouterProvider) {

        $urlRouterProvider.otherwise('/shopping');

        var userFactory = require('./common/user/userFactory');

        $stateProvider
            .state('shopping', {
                url: '/shopping',
                templateUrl: 'app/shopping.html',
                resolve:{
                    "check":function(){
                        var something = userFactory;
                        console.log(userFactory.name);
                        console.log(userFcatory);
                    }
                }
            });
    };

    module.exports = ['$stateProvider', '$urlRouterProvider', Router];
});

When console.log'ing:

console.log(userFactory.name);  //returns UserFactory
console.log(userFcatory);       //returns:

Object {name: "UserFactory", component: Array[4]}
    component:Array[4]
        0:"$q"
        1:"$http"
        2:"$state"
        3:($q, $http, $state)
        length:4
        __proto__:Array[0]
        name:"UserFactory"

However my getData and useData methods do not seem to be available for me to use?


Solution

  • Fixed this by changing my router.js to:

    define(['module', 'require'], function(module, require) {
        'use strict';
    
        var Router = function ($stateProvider, $urlRouterProvider) {
    
            $urlRouterProvider.otherwise('/shopping');
    
            var userFactory = require('./common/user/userFactory');
    
            $stateProvider
                .state('shopping', {
                    url: '/shopping',
                    templateUrl: 'app/shopping.html',
                    resolve:{
                        userFactory : 'UserFactory',
                        check:function(userFactory){
                            return userFactory.checkUser();
                        }
                    }
                });
        };
    
        module.exports = ['$stateProvider', '$urlRouterProvider', Router];
    });