Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS-如果url为';不正确_Angularjs - Fatal编程技术网

AngularJS-如果url为';不正确

AngularJS-如果url为';不正确,angularjs,Angularjs,我正在angularjs中构建一个不同状态的应用程序,并添加了一个$urlRouterProvider重定向人员是指他们没有进入我设置的状态 虽然我有一个问题,当他们去一个网址以外的我已经设置。 例如,以下状态是profile: .state('profile', { url: '/profile', templateUrl: 'partials/profile.html', controller: 'profileCtrl', data: {

我正在angularjs中构建一个不同状态的应用程序,并添加了一个
$urlRouterProvider重定向人员是指他们没有进入我设置的状态

虽然我有一个问题,当他们去一个网址以外的我已经设置。 例如,以下状态是profile:

.state('profile', {
      url: '/profile',
      templateUrl: 'partials/profile.html',
      controller: 'profileCtrl',
        data: {
            requiresLogin: true
        },
        resolve: {
          $title: function() { return 'Profile'; }
        }
    });
假设我使用/prof而不是/profile访问一个url,我将得到一个重定向到home aka(“/”)

这里是问题出现的地方:如果我转到一个url,如下所示:/profile/test,我的控制台会出现很多错误(下图)

额外信息(来自评论):

我还将给出服务器端代码,查看重定向可能存在的问题:

// Init Express Web Framework
var express = require('express');
var app = express();
var path = require('path');

// Set view engine to EJS & set views directory
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, 'client', 'views'));

app.use(express.static(path.resolve(__dirname, 'client')));

// Database Connection
var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');
mongoose.connect(configDB.url);

var bodyParser = require('body-parser');
app.use(bodyParser.json());

// Main route
app.get('/', function(req, res){
    res.render('index.ejs');
});

// API 
var api = express.Router();
require('./server/routes/capture')(api);
app.use('/api', api);

// Set routes to other pages
app.get('/*', function(req, res){
    res.render('index.ejs');
});

// Port Settings
app.listen(process.env.PORT || 3000, process.env.IP);
console.log('Listening on port ' + process.env.PORT);
这是我的工作区

重定向未列出的URL将被重定向。在向链接添加内容时,列出的Url(例如/dashboard,/profile,…)不会被重定向。当我没有将控制器添加到主html时,我遇到了这些错误,但这就是问题所在。。我不想浏览这些链接。app.js中未声明的所有链接都应重定向

有办法解决这个问题吗

编辑-添加404:

我使用以下代码在我的server.js中添加了catch 404:

    // catch 404 and forwarding to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});
结果是:

Error: Not Found
   at /home/ubuntu/workspace/server.js:31:15
   at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
   at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
   at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
   at jsonParser (/home/ubuntu/workspace/node_modules/body-parser/lib/types/json.js:100:40)
   at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
   at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
   at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
   at SendStream.error (/home/ubuntu/workspace/node_modules/express/node_modules/serve-static/index.js:120:7)
   at emitOne (events.js:77:13)
   at SendStream.emit (events.js:169:7)
   at SendStream.error (/home/ubuntu/workspace/node_modules/express/node_modules/send/index.js:245:17)

Unexpected token“你能制作一个Plunkr复制错误吗?制作一个完整的Plnkr代码来复制错误是没有用的,因为它是直接的。重定向未列出的URL会被重定向。列出的URL(例如/dashboard,/profile,…)在向链接添加内容时,它们不会被重定向。我在没有将控制器添加到主html时遇到了这些错误,但这就是问题所在。我不想浏览这些链接。所有未在app.js中声明的链接都应该被重定向。Unexpected Token“
Token”Unexpected Token文档中的所有脚本都已正确定义。我仍然可以转到正确的URL(当在app.js中声明时)。我还得到了一个重定向页面不存在。当我向声明的链接添加内容时,重定向不适用。我查了一下.htaccess,他们说当我添加´$locationProvider.html5Mode({enabled:true,requireBase:false})时,需要添加它;为了使url看起来干净,我的代码需要修改。这就是问题所在吗?只需打开“网络”选项卡并查看,您的脚本加载为
text/html
。检查它们的URL,尝试在新选项卡中打开
。显然,它们的URL是错误的。可能是一些相关的东西,比如
src=“scripts/angular.js”
解析为
http://localhost/reports/scripts/angular.js
/reports
页面上的
。此时甚至没有加载您的角度报告。这与此问题无关。使用404调试器更新OP,如上所述,它尚未找到,但我没有找到解决方案。只需在脚本URL中添加斜杠即可!