[英]Angular NgRoute "/" works but the rest don't?
I have an app running on the MEAN stack, and I can't seem to get any angular route other than "/" to work.我有一个在 MEAN 堆栈上运行的应用程序,除了“/”之外,我似乎无法获得任何角度路线。 I have tried this with both ng-Route and ui-router, and both keep sending anything other than "/" (like /contacts or /portfolio_item/4) back to "/".
我已经用 ng-Route 和 ui-router 尝试了这个,并且都继续将“/”以外的任何内容(如 /contacts 或 /portfolio_item/4)发送回“/”。
Here is app.js:这是 app.js:
// using the express library in variable express
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// specify that app is a new express application
var app = express();
app.use('/javascripts', express.static(__dirname + '/public/javascripts'));
app.use('/images', express.static(__dirname + '/public/images'));
app.use('/stylesheets', express.static(__dirname + '/public/stylesheets'));
app.use('/pages', express.static(__dirname + '/public/pages'));
app.use('/partials', express.static(__dirname + '/views/partials'));
app.use('/portfolio.json', express.static(__dirname + 'portfolio.json'));
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
And here is angularapp.js:这是 angularapp.js:
var myApp = angular.module('myportfolio', ['ngRoute'])
myApp.config(function($routeProvider){
$routeProvider
.when('/', {
templateUrl: '/pages/home.html',
controller: 'mainController'
})
.when('/portfolio_item/:item_number', {
templateUrl: '/pages/item.html',
controller: 'portfolioController'
})
.when('/contacts', {
templateUrl: '/pages/contact.html',
controller: 'contactController'
})
.otherwise({ redirectTo: '/' });
Regardless of what I do, both /portfolio_item/5 and /contacts keep showing me /pages/home.html... it's odd to me that only the index would work.不管我做什么,/portfolio_item/5 和/contacts 都不断地向我展示/pages/home.html……对我来说,只有索引才有效,这很奇怪。 When I removed the ".when('/', {" portion, it would just show me the layout with no view at all from any URL.
当我删除 ".when('/', {" 部分时,它只会向我显示任何 URL 中根本没有视图的布局。
I am working on a C9 environment and running a Node server if that makes a difference?我正在 C9 环境中工作并运行 Node 服务器,如果这有区别的话?
If you check your browser console, you should find an error of this form:如果您检查浏览器控制台,您应该会发现以下形式的错误:
Uncaught SyntaxError: Unexpected token <
未捕获的语法错误:意外的标记 <
The error occurs because the server is not serving the static files.发生错误是因为服务器没有提供静态文件。
express.static
is being used as a middleware for routes on the server. express.static
被用作服务器express.static
由的中间件。
To correct this, you need to include the line:要更正此问题,您需要包含以下行:
app.use(express.static(__dirname + '/public'));
This serves all the static files in the public folder.这为公共文件夹中的所有静态文件提供服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.