使用URL参数时控制器不会触发

时间:2014-02-08 13:27:00

标签: angularjs routes

我不清楚为什么这不起作用。我的其余代码 - 没有基于URL Param的路由工作正常,我已经测试了所有。但是,当我尝试在我的url(base.url / route /:param)中包含一个URL参数时,我的routeProvider的'.otherwise'元素正在触发,而不是在routeProvider中指定的相应控制器。

以下是相关的代码:

module.config

app.config(function($routeProvider){
//http://docs.angularjs.org/tutorial/step_07
$routeProvider
    .when('/home',
        {
            templateUrl: 'app/partials/home.html',
            controller: 'HomeController'
        })
    .when('/implementation-reference/:ref-scope',
        {
            templateUrl: 'app/partials/topic_tpl.html',
            controller: 'ImplReference'
        })
    .when('/projects',
        {
            templateUrl: 'app/partials/project_list_tpl.html',
            controller: 'Projects'
        })
    .when ('/site-help',
        {
            templateUrl: 'app/partials/site-help.html'
        })
    .otherwise(
        {
        templateUrl: 'app/partials/404.html'
        })
});

module.controller

app.controller('ImplReference', function($scope, $routeParams) {
alert('hi');    
});

进入/ implementation-reference / whatever时,我得到了404页面。

为什么我没有看到我在控制器开头发出警报的任何想法,也没有在控制台中看到任何错误?

1 个答案:

答案 0 :(得分:0)

尽我所知,问题在于在RouteProvider中定义的param中使用破折号。我最初有:

.when('/implementation-reference/:ref-scope',
    {
        templateUrl: 'app/partials/topic_tpl.html',
        controller: 'ImplReference'
    })

当我把它更改为:

.when('/implementation-reference/:ref',
    {
        templateUrl: 'app/partials/topic_tpl.html',
        controller: 'ImplReference'
    })

......突然一切正常。

在向'else'添加控制器之后我注意到了这一点,它向我显示了$ route信息。在钻探之后,我看到预定义路线上的正则表达式只有在破折号之前的部分工作。

我已经被这个困惑了好几个小时,无法想象在某个地方如何更清楚地记录这一点。也许这是一些新手的错误(我是新手)。