使用In Resolve

时间:2016-04-29 09:11:13

标签: angularjs angular-ui-router angularjs-service angularjs-factory angular-ui-router-extras

我正在尝试在我的ui-router解决方案中使用工厂。我可以看到我的工厂名称被返回,但是这些方法不会出现在那里:

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];
});

当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"

但我的getDatauseData方法似乎无法供我使用?

1 个答案:

答案 0 :(得分:0)

通过将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:{
                    userFactory : 'UserFactory',
                    check:function(userFactory){
                        return userFactory.checkUser();
                    }
                }
            });
    };

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