因此,angularjs $routeProvider 的工作流程如下,
$routeProvider
.when('/main1', {
templateUrl: 'view1.html',
})
.when('/main2', {
templateUrl: 'view2.html',
})
我的问题是,有什么方法可以简化以下代码。如果 when('/main1') 和 when('/main2') 指向同一个模板。所以,
$routeProvider
.when('/main1', {
templateUrl: 'view1.html',
})
.when('/main2', {
templateUrl: 'view1.html',
})
之所以提出这个问题,是因为如果我们在网站上有多种语言,并且我们希望对 url 进行多种翻译。
另一种解决方案是识别站点是使用 .com 还是 .de,从而调整到正确的 /main1 或 /main2 翻译。例如,
var url = window.location.href;
var main;
if (url.match(/.de/) !== null){
main = "/main1";
}else{
main = "/main2";
}
$routeProvider
.when(main, {
templateUrl: 'view1.html',
})
但从语义上讲,这似乎不是最好的解决方案,因为我喜欢在配置之后将配置选项设置在运行块中。我们也不能注入工厂(只有提供者,我可能弄错了)来配置。