0

angular-translate は HTML の変更に取り組んでいます。変更する必要がある ngDropdown ボタン ディレクティブの選択項目の配列があります。言語を変更した後、translate.use() を使用して新しい/現在の言語を取得しようとします。何かがうまくいきません。

ここにHTMLがあります

<div id="postBox" class="floatingSection" data-ng-controller="postButtonController2">
  <button id="postButton" dropdown-menu="ddMenuOptions" dropdown-model="ddMenuSelected" class="btn-menu" ng-click="getCurrentLanguage()">{{ 'POST' | translate }}</button>
</div>

これがその postButton のコントローラーです。$translate から新しい/現在の言語を取得し、その文字列を使用してドロップダウン選択配列を取得する必要があります。ddSelections.withLangChoice $scope.getCurrentLanguage は、ハードコードされている場合は正しい配列を選択しますが、言語文字列の $translate から変数を取得する場合は選択しません。

ドロップダウン付きのボタンのコントローラーを次に示します。コメントは、機能するものと機能しないものを数行にわたって説明します。

residenceApp.controller('postButtonController2', ['$translate', '$scope', 'changePostDdFactory',
function ( $translate, $scope, ddSelections ){
//ddSelections is the result object from the function returned by changePostDdFactory
  $scope.getCurrentLanguage = function( $translate ){
    alert('here I am in postButtonController2'); //fires on post button clicks and on page load
    alert('here I am in postButtonController2' + $translate.use()); //does not fire on post button clicks. fires on page load
    $scope.langKey=$translate.use(); //gets langKey in $translate on page load only
    $translate.refresh($scope.langKey);//should refresh the translation, but nothing
    alert('from postButtonController2 - currentLanguage is' + ' ' + $translate.use() ); //does not fire on button click. fires on page load
    //return 'es'; //hardcoded works on page load & sets ddSelections.withLangChoice to es array
    return $translate.use(); //sets language for ddSelections on page load, no language changes
};
$scope.ddMenuOptions = ddSelections.withLangChoice($scope.getCurrentLanguage($translate)); //works on page load, no language change occurs
$scope.ddMenuSelected = {};
//working code to watch for change from user selection
}]);

私は新しいのですが、コントローラーで $translate サービスが動作するべきではありません。それが本当であると仮定すると、変更を行うには何を変更する必要があります。最近、ボタンの HTML に ng-click を追加しましたが、それ自体には何の影響もありませんでした。

4

1 に答える 1

0

OPの2行目から最後の行が実行されていないのではないかと疑い始めました。実行される前の行、実行された後の行ですが、その周りの alerts() は奇妙でした。だから私はその行を関数内に入れました、そしてそれはうまくいきました。これが改訂された作業コードです。

residenceApp.controller('postButtonController2', ['$translate', '$scope',  'changePostDdFactory',
function ( $translate, $scope, ddSelections ){
  var ddSelections;
  //ddSelections is the result object from the function returned by changePostDdFactory
  $scope.getCurrentLanguage = function(){
  $scope.langKey=$translate.use(); //use() as a getter
  $scope.ddMenuOptions = ddSelections.withLangChoice($scope.langKey);
  return;
};
$scope.ddMenuSelected = {};
//code to watch for change from user selection
}]);

$scope.getCurrentLanguage が無名関数としてのみ機能する理由と、$scope.ddMenuOptions が関数内にある必要がある理由について、私は依然として戸惑っています。しかし、それは機能し、非常に簡単です。

于 2014-09-16T17:22:52.690 に答える