我正在使用angularJS-transtale,我想更改配置部分中的值

时间:2018-01-25 10:19:45

标签: javascript angularjs angular-translate

我已经构建了一个小型应用程序来切换语言

我创建了一个控制器,我从ng-model获取值,然后将值赋给变量。我需要帮助才能在app.config中传递此值。

代码:



var app = angular.module("myApp", ['pascalprecht.translate']);
var name = 'Anuj';
app.controller("translateController", ["$scope", "$translate", function($scope, $translate) {

  $scope.changeValue = function() {
    name = $scope.textname;
    $translateProvider.translations('en', en_translations);
  }
  $scope.changeLanguage = function(lang) {
    $translate.use(lang);
  }

}]);
app.config(["$translateProvider", function($translateProvider) {

  var en_translations = {
    "language": name,
    "greeting": "Welcome Dinesh"
  }

  var sp_translations = {
    "language": "Selected Language Spanish",
    "greeting": "Bienvenida Dinesh"
  }

  $translateProvider.translations('en', en_translations);

  $translateProvider.translations('sp', sp_translations);

  $translateProvider.preferredLanguage('en');

}]);

<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <script data-require="angular.js@1.3.8" data-semver="1.3.8" src="https://code.angularjs.org/1.3.8/angular.js"></script>
  <script data-require="angular-translate@*" data-semver="2.5.0" src="https://cdn.rawgit.com/angular-translate/bower-angular-translate/2.5.0/angular-translate.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body>
  <div ng-controller="translateController">
    <h1>Demo for angularJS translator</h1>
    <input type="text" name="" ng-model="textname" /> {{textname}}
    <button ng-click="changeValue()">Change</button>
    <div>
      <button ng-click="changeLanguage('en')">English</button>
      <button ng-click="changeLanguage('sp')">Spanish</button>
    </div>

    <h2>{{'language' | translate}}</h2>

    Hello!!!

    <p>{{'greeting' | translate}}</p>

  </div>
</body>

</html>
&#13;
&#13;
&#13;

您可以在plunker

中查看代码

0 个答案:

没有答案