【发布时间】:2017-09-04 08:47:23
【问题描述】:
我需要将一个变量从服务器端传递给 AngularJS。
我在服务器端有以下 HTML
<div ng-app="tablesApp" ng-controller="tablesCtrl" ng-init="lang='@lang';...go();" ...>
<st-date-range ?lang="@lang"? ...> </st-date-range>
...
</div>
我应该在 HTML 代码中的某个位置(实际上是在 ng-init,但如果有其他选项我可以接受)我的服务器端 @lang 值,那么 Angular 应该使用该值......
我使用一个指令,我想将@lang(服务器端 ASP.NET razor 变量)参数传递给 angular,以便在模板路径中使用它:
app.directive('stDateRange', [function () {
return {
restrict: 'E',
require: '^stTable',
templateUrl: '/templates/stDateRange.en.html',
scope: false,
link: function (scope, element, attr, ctrl) {
var tableState = ctrl.tableState();
scope.$watchGroup(["minDate", "maxDate"],
function (newValues, oldValues) {
所以,我的服务器端 @lang 参数我想传递给指令以便在模板 URL 中使用它,如下所示:
templateUrl: '/templates/stDateRange.@(lang).html'
附注:
我会用this codepen example 来表达我的需要:
var app = angular.module('app', []);
app.directive('testDirective', function(){
var lang = 'en'; // <<< Set the variable HERE << !!!
return {
restrict: 'E',
template: '<p>my lang is "<strong>'+lang+'</strong>" </p>'
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="app" ng-init="lang='fr'">
<h3>Test directive for 'fr' lang</h3>
<test-directive></test-directive>
</section>
【问题讨论】:
标签: angularjs internationalization