【问题标题】:UI-Router not changing state when using $state.go()UI-Router 在使用 $state.go() 时不改变状态
【发布时间】:2014-06-18 16:58:49
【问题描述】:

我正在为我的 ui-router 路由编写单元测试。但是,我不能让它通过。

这是我的测试,$state.current.name 给了我frontpage,这是otherwise 状态。

it('should change state to items', function() {
    $state.go('items');

    $rootScope.$digest();

    expect($state.current.name).toBe('items');
})

状态:

$urlRouterProvider.otherwise("/");

$stateProvider
    .state('items', {
        url: '/items',
        templateUrl: 'js/modules/items/partials/items.html',
        controller: 'ItemsController',

        resolve: {
            items : ['ItemService', '$q', function(ItemService, $q) {

                var defer = $q.defer();

                ItemService.getItems().success(function(data) {

                    defer.resolve(data);

                });

                return defer.promise;
            }]
        }

    })

    .state('frontpage', {
        url: '/',
        template: 'Frontpage'
    })

为什么我在调用$state.go('items') 后得到frontpage 状态?

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine angular-ui-router


    【解决方案1】:

    注入$q,然后尝试$q.flush();改变状态后。

    【讨论】:

      【解决方案2】:

      花了几个小时后,我最终使用了$timeout.flush(),我猜这是清除所有承诺队列。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-03
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-01
        相关资源
        最近更新 更多