Javascript Angularjs routconfig用于不同于中指定的页面
在angularjs中,通常有以下类型的路由配置:Javascript Angularjs routconfig用于不同于中指定的页面,javascript,angularjs,Javascript,Angularjs,在angularjs中,通常有以下类型的路由配置: app.config(['$routeProvider', function($routeProvider) { //Setting up HTML pages and controllers depending upon the suffix in the URL $routeProvider. when('/xyz', { templateUrl: '/etc/
app.config(['$routeProvider',
function($routeProvider) {
//Setting up HTML pages and controllers depending upon the suffix in the URL
$routeProvider.
when('/xyz', {
templateUrl: '/etc/topproducts.html',
controller: 'CategoryListCtrl'
}).
when('/abc/:alphabet?', {
templateUrl: '/etc/allproducts.html',
controller: 'CategoryListCtrl'
}).
otherwise({
redirectTo: '/xyz'
});
}
]);
所以,若用户转到主页,那个么他会被重定向到topproducts.html,若他/她在url中键入/abc,那个么在我的例子中,用户会显示allproducts.html
我的问题是,如果用户在url(如/blahblah)中键入意外路径,如何控制显示哪个页面
因为在我上面的配置中,否则{指向xyz,用户将登录到topproducts.html。我希望当用户键入/blahblah和/时,用户将显示不同的页面,您的代码中已经有它:
otherwise({
redirectTo: '/xyz'
});
这部分意味着,如果用户键入的内容与您在路由配置中指定的内容不同,他将被重定向到/xyz
请执行以下操作:
when('/unexpected', {
templateUrl: '/unexpected.html',
controller: 'UnexpectedCtrl'
}).
otherwise({
redirectTo: '/unexpected'
})
可用方法1:
我希望你能在angular应用程序中使用angular ui Router而非默认值一次。如果仍然可行,那么你可以转换它。他们确实有一个可用的功能来实现你想要实现的目标。你可以通过这种方式轻松地处理它
$urlRouteProvider.rule:这里为您提供文档以及如何使用它
可用方法2:
使用现有的默认urlRouterProvider,进行如下配置
。当“/”和时,$routeParams将包含用户在url中附加的所有内容,该url在内部监视$location
路径可以包含以冒号开头的命名组:例如:name.All
直到下一个斜杠的字符将被匹配并存储在$routeParams中
当路由匹配时,在给定名称下。路径可以包含命名的
以冒号开头并以星形结尾的组:例如:name*.All
字符急切地存储在给定名称下的$routeParams中
当路线匹配时
使用$routeParams进行演示
App.controller("ChildCtrl1",function ($scope,$routeParams) {
$scope.message = "Link 1 has been Visited";
$scope.debug = $routeParams;
});
若我能理解你们的问题,那个么你们想显示未找到的页面,若那个Url或页面在配置中不存在 试试下面
app.config(['$routeProvider',
function($routeProvider) {
//Setting up HTML pages and controllers depending upon the suffix in the URL
$routeProvider.
when('/xyz', {
templateUrl: '/etc/topproducts.html',
controller: 'CategoryListCtrl'
}).
when('/abc/:alphabet?', {
templateUrl: '/etc/allproducts.html',
controller: 'CategoryListCtrl'
}).
otherwise({
templateUrl: myLocalized.partials + '404.html',
controller: '404'
});
}
]);
您的404控制器将
app.controller('404', function() {
document.querySelector('title').innerHTML = 'Page not found | AngularJS Demo Theme';
});
模板可以是
<h1>Page Not Found</h1>
<p>Sorry, but nothing can be found at this location.</p>
请尝试此操作,并让我知道它是否适用于您否则适用于意外路径。请将url重定向到Other中,并在该url中定义您希望显示意外路径的页面。但angular不会告诉我用户键入的/blahblah。url将由angular从/blahblah更改为/xyz。您认为可以通过在cl中添加一个来完成此操作吗有正则表达式的东西吗?明白你的意思了。你不想更改用户在url中键入的url。你想在该url显示一些内容。但是angular不会告诉我用户键入的/blahblah。url将由angular从/blahblah更改为/xyz。你认为可以通过添加一个带有正则表达式的when子句来完成吗关于东西?@Sandeep,不确定你想做什么,但还是看看我的更新。如果这不是你想要的,你能澄清你到底想做什么吗?