UI路由器$ state.go抛出$ stateChangeError

时间:2017-02-15 20:07:39

标签: angularjs angular-ui-router

当我尝试运行$state.go();

时,UI-Router v0.4.2遇到了以下错误,我遇到了一个问题(我认为)
  

14:57:22.563错误:[$ http:badreq] Http请求配置url必须是字符串。收到:[“$ stateParams”,null]   http://errors.angularjs.org/1.5.8/ $ HTTP / badreq?P0 =%5B%22%24stateParams%22%2Cnull%5D   堆栈跟踪:   minErr /< @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:68:12   $ HTTP @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:11439:15   createShortMethods / HTTP://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:11698:18   handleRequestFn @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:19419:14   TemplateFactory/this.fromUrl@http://localhost:3001/vendor.js:1488:16   TemplateFactory/this.fromConfig@http://localhost:3001/vendor.js:1444:39   负荷@ http://localhost:3001/vendor.js:4586:20   resolveViews / HTTP://本地主机:3001 / vendor.js:4479:20   调用@ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:4718:16   继续@ http://localhost:3001/vendor.js:1266:32   调用@ http://localhost:3001/vendor.js:1262:26   $解决/ this.study /< @ http://localhost:3001/vendor.js:1241:14   $Resolve/this.resolve@http://localhost:3001/vendor.js:1345:12   resolveViews /< @ http://localhost:3001/vendor.js:4482:30   的forEach @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:335:11   resolveViews @ http://localhost:3001/vendor.js:4476:9   processQueue @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:16383:28   scheduleProcessQueue /< @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:16399:27   。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17682:16   。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17495:15   。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17790:13   ngEventHandler /< @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:25890:17   defaultHandlerWrapper @ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:3497:3   createEventHandler /事件处理程序@ http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:3485:9   1 script.js:1301:13   http://localhost:3001/app/script.js:1301:13     。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:18005:15     transitionTo / $ state.transition<   http://localhost:3001/vendor.js:4245:15 processQueue   http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:16383:28     scheduleProcessQueue /<   http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:16399:27     。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17682:16     。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17495:15     。$ RootScopeProvider /这$ gethttp://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:17790:13     ngEventHandler /<   http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:25890:17     defaultHandlerWrapper   http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:3497:3     createEventHandler /事件处理程序   http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js:3485:9

路线如下:

.state('foo.bar', {
    url: '/:type/:id',
    views: {
        'question': {
            templateUrl: ['$stateParams', function (stateParams) {
                var qID = stateParams.id;
                var template = qID + '.tmpl.html';
                var url = "app/section/foo/";

                return qID <= 7 ? url + 'general/' + template : url + stateParams.type + "/" + template;
            }]
        }
    }
})

我正在使用它来尝试去州:

$state.go('foo.bar', {
    type: 'typeA',
    id: 1
});

1 个答案:

答案 0 :(得分:0)

[插入脸掌]

templateUrl上的方形括号:导致问题

templateUrl: function (stateParams) {
    var qID = stateParams.id;
    var template = qID + '.tmpl.html';
    var url = "app/section/foo/";

    return qID <= 7 ? url + 'general/' + template : url + stateParams.type + "/" + template;
}