简体   繁体   English

Angular Controller在路线参数上返回404

[英]Angular Controller returns 404 on a Route Parameter

I'm building my first web app with Node.js and AngularJs. 我正在使用Node.js和AngularJs构建我的第一个Web应用程序。 My problem lies in this code here: 我的问题在于此代码:

var app = angular.module('Martin', ['ngResource','ngRoute']);

app.config(['$routeProvider', function($routeProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'partials/home.html',
            controller: 'PageCtrl'
        })
        .when('/etx/:id', {
            templateUrl: 'partials/page.html',
            controller: 'PageCtrl'
        });
}]);

app.controller('PageCtrl', ['$scope', '$resource', '$routeParams',
    function($scope, $resource, $routeParams){
        console.log('Got here');
        var Page = $resource('/api/pages/:id');
        Page.get({ id: $routeParams.id }, function(page){
            $scope.page = page;
        });
    }]);

From what I understand, if I were to request /etx/mypage, the function in PageCtrl should be run, but when I try to pull it up in a browser, I get a 404 error and nothing is printed to the console. 据我了解,如果我请求/ etx / mypage,则应该运行PageCtrl中的函数,但是当我尝试在浏览器中将其拉出时,出现404错误,并且控制台上没有任何内容。

I tested it with the home page ('/'), and the controller works fine then. 我用主页('/')测试了它,然后控制器工作正常。

As @Claies identified, the problem was I forgot the octothorpe (or hashtag if you want to be less cool) in the URL. 正如@Claies所指出的那样,问题是我忘记了网址中的octothorpe(如果不想太酷的话,也可以使用标签标签)。 So it should not be etc/mypage , but #/etc/mypage . 因此它不应该是etc/mypage ,而应该是#/etc/mypage This behavior is less than ideal in many cases (including mine), so I recommend disabling this with HTML5 mode for links . 在许多情况下(包括我的情况),这种行为都不理想,因此我建议使用HTML5模式对links禁用此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM