Angular 使用 ngRoute 进行路由不适用于某些路由
Angular Routing with ngRoute doesn't work for some routes
在我的单页应用程序中,我使用 ngRoute 进行 angular 路由。但是我在 angular 路由方面遇到了一些问题。
配置:
app.config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when('/product',{
templateUrl : 'views/product/template/product.html',
controller : 'productCtrl'
})
.when('/product/add',{
templateUrl : 'views/product/template/add-product.html',
controller : 'productCtrl'
})
});
当我的路由路径是 /product 时一切正常。但是当我的路由路径是 /product/add 时,它似乎是一个错误。但是如果“/product/add”
仅替换为“/add”,那么该路线也可以。所以我无法识别路由“/product/add”的主要问题是什么。
错误:
http://localhost:3000/product/views/product/template/add-product.html 404 (Not Found)
这里我看到了一个问题。其他路线似乎是:
http://localhost:3000/views/product/template/product.html
但错误路由在服务器 link 之后有 /product。
需要对此问题进行一些说明和解决方案。
提前致谢
使用错误页面正确配置路由。您可以按照此 fiddle 示例进行操作。
HTML
<div ng-controller="MainCtrl">
<ul>
<li><a href="/product">product</a></li>
<li><a href="/product/add">add</a></li>
<li><a href="/other">Other</a></li>
</ul>
<ng-view></ng-view>
</div>
Angular
var app = angular.module( "myApp", [] );
app.config( function ( $routeProvider,$locationProvider ) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when( '/product', { template: 'I am product root' } )
.when( '/product/add', { template: 'Adding a product' } )
.when( '/other', { template: 'Other pages' } )
.otherwise( { redirectTo: '/product' } );
});
app.controller( 'MainCtrl', function ( $scope ) {
});
更多https://jsfiddle.net/zahiruldu/jLdce3vj/186/
您可以使用 UI 路由器正确处理父路由,这将为您提供先进的嵌套路由功能。
在我的单页应用程序中,我使用 ngRoute 进行 angular 路由。但是我在 angular 路由方面遇到了一些问题。
配置:
app.config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when('/product',{
templateUrl : 'views/product/template/product.html',
controller : 'productCtrl'
})
.when('/product/add',{
templateUrl : 'views/product/template/add-product.html',
controller : 'productCtrl'
})
});
当我的路由路径是 /product 时一切正常。但是当我的路由路径是 /product/add 时,它似乎是一个错误。但是如果“/product/add” 仅替换为“/add”,那么该路线也可以。所以我无法识别路由“/product/add”的主要问题是什么。
错误:
http://localhost:3000/product/views/product/template/add-product.html 404 (Not Found)
这里我看到了一个问题。其他路线似乎是:
http://localhost:3000/views/product/template/product.html
但错误路由在服务器 link 之后有 /product。
需要对此问题进行一些说明和解决方案。
提前致谢
使用错误页面正确配置路由。您可以按照此 fiddle 示例进行操作。
HTML
<div ng-controller="MainCtrl">
<ul>
<li><a href="/product">product</a></li>
<li><a href="/product/add">add</a></li>
<li><a href="/other">Other</a></li>
</ul>
<ng-view></ng-view>
</div>
Angular
var app = angular.module( "myApp", [] );
app.config( function ( $routeProvider,$locationProvider ) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when( '/product', { template: 'I am product root' } )
.when( '/product/add', { template: 'Adding a product' } )
.when( '/other', { template: 'Other pages' } )
.otherwise( { redirectTo: '/product' } );
});
app.controller( 'MainCtrl', function ( $scope ) {
});
更多https://jsfiddle.net/zahiruldu/jLdce3vj/186/
您可以使用 UI 路由器正确处理父路由,这将为您提供先进的嵌套路由功能。