角度路线未显示视图

时间:2016-06-16 00:37:53

标签: angularjs angular-routing

我遇到了使路线工作的问题:

angular_app.config(function($routeProvider){
    $routeProvider.when('Title', {
        controller : 'TitleController',
        templateUrl : 'app/html/Title.html'
    }).otherwise({
        redirectTo : 'Title'
    });
});

这不会正确启动,但如果我这样编辑:

angular_app.config(function($routeProvider){
    $routeProvider.when('/', {
        controller : 'TitleController',
        templateUrl : 'app/html/Title.html'
    }).otherwise({
        redirectTo : 'Title',
        controller : 'TitleController',
        templateUrl : 'app/html/Title.html'
    });
});

它会激活我的控制器,但它会执行两次。为什么它不适用于第一种情况?。

我的应用程序将在我的Title.html文件中显示它的入口点。

任何提示?

2 个答案:

答案 0 :(得分:2)

需要仔细阅读文档。

第一个参数必须是带有前导/的路径。此外,redirectTo也应该是已在when()

中声明的路径
angular_app.config(function($routeProvider){
    $routeProvider.when('/title', {
        controller : 'TitleController',
        templateUrl : 'app/html/Title.html'
    }).otherwise({
        redirectTo : '/title'            
    });
});

答案 1 :(得分:0)

您应该将代码更改为此。

angular_app.config(function($routeProvider){
$routeProvider.when('/title', {
    controller : 'TitleController',
    controllerAs: 'Title',
    templateUrl : 'app/html/Title.html'
}).otherwise({
    redirectTo : '/title'            
});});