如何在AngularJS中加载特定于语言的模板?

如何在AngularJS中加载特定于语言的模板?,angularjs,Angularjs,我的路线定义为 $routeProvider.when('/:culture/:gameKey/:gameId/closed',{templateUrl:'/templates/tradingclosed',controller:TradingClosedCtrl}) 我希望在以某种方式请求模板时包含“culture”参数,这样我就可以提供翻译后的模板 这是可能的吗?如果我读得正确,您希望以某种方式使用url路由中的区域性参数来确定检索模板的位置 可能有更好的方法,但描述了使用ng includ

我的路线定义为

$routeProvider.when('/:culture/:gameKey/:gameId/closed',{templateUrl:'/templates/tradingclosed',controller:TradingClosedCtrl})

我希望在以某种方式请求模板时包含“culture”参数,这样我就可以提供翻译后的模板


这是可能的吗?

如果我读得正确,您希望以某种方式使用url路由中的
区域性
参数来确定检索模板的位置

可能有更好的方法,但描述了使用
ng include
在集中式控制器内检索
$routeParams
以动态加载视图

类似于此:

angular.module('myApp', []).
    config(function ($routeProvider) {
        $routeProvider.when('/:culture/:gameKey/:gameId/closed', {
            templateUrl: '/templates/nav/urlRouter.html',
            controller: 'RouteController'
        });
    });

function RouteController($scope, $routeParams) {
        $scope.templateUrl = 'templates/tradingclosed/' + $routeParams.culture + '_template.html';
    }
将此作为您的urlRouter.html

<div ng-include src="templateUrl"></div>

如果我没有看错,您可能希望以某种方式使用url路由中的
区域性
参数来确定检索模板的位置

可能有更好的方法,但描述了使用
ng include
在集中式控制器内检索
$routeParams
以动态加载视图

类似于此:

angular.module('myApp', []).
    config(function ($routeProvider) {
        $routeProvider.when('/:culture/:gameKey/:gameId/closed', {
            templateUrl: '/templates/nav/urlRouter.html',
            controller: 'RouteController'
        });
    });

function RouteController($scope, $routeParams) {
        $scope.templateUrl = 'templates/tradingclosed/' + $routeParams.culture + '_template.html';
    }
将此作为您的urlRouter.html

<div ng-include src="templateUrl"></div>
我已经发布了@Gloopy建议的解决方案

如果没有ng include,您无法实现该功能的原因是,路由是在“配置”块中完成的,您不能在其中注入任何值(您可以在模块加载和依赖项部分中了解这些块

如果您不想引入新的作用域,可以使用我的ng include指令的精简版本替换ng include,该指令的作用域与ng include完全相同,但不创建新的作用域:

希望该解决方案能满足您的用例。

我已经发布了@Gloopy建议的解决方案

如果没有ng include,您无法实现该功能的原因是,路由是在“配置”块中完成的,您不能在其中注入任何值(您可以在模块加载和依赖项部分中了解这些块

如果您不想引入新的作用域,可以使用我的ng include指令的精简版本替换ng include,该指令的作用域与ng include完全相同,但不创建新的作用域:


希望该解决方案能满足您的使用情况。

谢谢:)遗憾的是,如果没有include,这是不可能的(节省了资源负载,还可能节省了一些性能)。我选择不接受答案,因为在使用包含功能时,它似乎会对作用域造成干扰,所以我仍在寻找一种很好的方法。谢谢:)遗憾的是,如果没有包含,这是不可能的(节省了资源负载,可能会带来一些性能)。我选择不接受答案,因为在使用include功能时,它似乎会对作用域造成干扰,所以我仍在寻找一种很好的方法来实现这一点。