Javascript 角度js-路由ui添加默认参数
在我的应用程序中,我使用AngularUI路由器 我有当地人(英语和希伯来语) 我的基础语言是英语 这就是为什么我希望如果语言是英语,不要将参数添加到url 例如:Javascript 角度js-路由ui添加默认参数,javascript,angularjs,angular-ui-router,angular-ui,Javascript,Angularjs,Angular Ui Router,Angular Ui,在我的应用程序中,我使用AngularUI路由器 我有当地人(英语和希伯来语) 我的基础语言是英语 这就是为什么我希望如果语言是英语,不要将参数添加到url 例如: 家庭英语-->http://example.com/ 家庭希伯来语-->http://example.com/he/ 关于我们英语-->http://example.com/about 关于我们希伯来语-->http://example.com/he/about 这可能吗 这是我当前的代码 $stateProvider
- 家庭英语-->
http://example.com/
- 家庭希伯来语-->
http://example.com/he/
- 关于我们英语-->
http://example.com/about
- 关于我们希伯来语-->
http://example.com/he/about
$stateProvider
.state('/', {
url: "/",
templateUrl: "Assets/app/templates/home.html",
controller: 'homeController'
})
.state('activity', {
url: "/activity",
templateUrl: "Assets/app/templates/gallery.html",
controller: 'galleryController'
})
.state('page', {
url: '/:pagename',
templateUrl: "Assets/app/templates/page.html",
controller: 'pageController'
});
有
像往常一样,这是可行的UI路由器
-内置功能。首先,我们将介绍超级父状态,例如“root”。它将定义参数lang
.state('root', {
url: '/{lang:(?:en|he|cs)}',
abstract: true,
template: '<div ui-view=""></div>',
params: {lang : { squash : true, value: 'en' }}
})
阅读
此外,我们还受益于名为参数:{}
的设置。它说,默认值是'en'
,更重要的是,如果与'en'参数值匹配,则应将其压扁并跳过:
params: {lang : { squash : true, value: 'en' }}
读或
因此,这是我们的父级,根状态,我们只需将其应用于具有状态定义设置的所有状态父级:'root'
:
.state('home', {
parent: 'root', // parent will do the magic
url: "/",
templateUrl: "Assets/app/templates/home.html",
controller: 'homeController'
})
.state('activity', {
parent: 'root', // parent magic
url: "/activity",
templateUrl: "Assets/app/templates/gallery.html",
controller: 'galleryController'
})
.state('page', {
parent: 'root', // magic
url: '/page/:pagename',
templateUrl: "Assets/app/templates/page.html",
controller: 'pageController'
});
现在这些链接将起作用:
ui-sref英语:
<a ui-sref="home({lang: 'en'})">home({lang: 'en'})</a>
<a ui-sref="activity({lang: 'en'})">activity({lang: 'en'})</a>
<a ui-sref="page({pagename:'one', lang: 'en'})">page({pagename:'one', lang: 'en'})</a>
<a href="#/">#/</a>
<a href="#/activity">#/activity</a>
<a href="#/page/three">#/page/three</a>
href希伯来语:
<a ui-sref="home({lang: 'he'})">home({lang: 'he'})</a>
<a ui-sref="activity({lang: 'he'})">activity({lang: 'he'})</a>
<a ui-sref="page({pagename:'two', lang: 'he'})">page({pagename:'two'})</a>
<a href="#/he/">#/he/</a>
<a href="#/he/activity">#/he/activity</a>
<a href="#/he/page/three">#/he/page/three</a>
检查一下
<a href="#/he/">#/he/</a>
<a href="#/he/activity">#/he/activity</a>
<a href="#/he/page/three">#/he/page/three</a>