Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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
Javascript angular 404不适用于模板URL或重定向到_Javascript_Angular - Fatal编程技术网

Javascript angular 404不适用于模板URL或重定向到

Javascript angular 404不适用于模板URL或重定向到,javascript,angular,Javascript,Angular,试图添加一个简单的404.html模板,但由于某些原因,无法呈现该模板。在我的app.routes.js中,我尝试了以下方法 $routeProvider.when('/', { templateUrl: '/templates/index.html', controller: 'IndexCtrl' }).otherwise({ templateUrl: '/templates/404.html' }) 我还尝试使用如下重定向,但它不起作用 .otherwise({

试图添加一个简单的404.html模板,但由于某些原因,无法呈现该模板。在我的app.routes.js中,我尝试了以下方法

$routeProvider.when('/', {
    templateUrl: '/templates/index.html',
    controller: 'IndexCtrl'
}).otherwise({
    templateUrl: '/templates/404.html'
})
我还尝试使用如下重定向,但它不起作用

.otherwise({
    redirectTo: '/templates/404.html'
})

当我尝试在控制台中键入类似的内容时,我看到404响应,但并没有呈现模板。是否需要调整控制器?

您可以使用特定视图为404创建特定路由
/templates/404.html

然后,在
否则()
方法调用中,您可以重定向到该路由
/404

app.config(function ($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'templates/index.html',
      controller: 'IndexCtrl'
    })
    .when('/404', {
      templateUrl: 'templates/404.html'
    })
    .otherwise({
      redirectTo: '/404'
    });
});

重定向到
需要先前定义的有效路由路径,而不是模板刚刚尝试了此操作,但仍然无法工作,所有其他页面正常工作我复制了您的代码,但什么也没有发生…它指向正确的模板,但由于某些原因它不起作用。在控制台中,如果我尝试使用404.html而不是/it works的index.html,我也会看到404。如果/it worksIf按您的设置工作,那么解决方案中发布的代码就会工作;)我需要为它添加控制器吗?