Can't get $translate service from angular-translate to change dropdown array for ngDropdowns

谁都会走 提交于 2020-01-06 08:52:35

问题


angular-translate is working to change my HTML. I have an array of selection items for the ngDropdown button directive that also has to change. After changing languages, I try to get the new/current language with translate.use(). Something doesn't work.

Here's the 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>

Here's the controller for that postButton. It should get the new/current language from $translate and use that string to get the dropdown selection array, ddSelections.withLangChoice $scope.getCurrentLanguage selects the correct array when hardcoded, but not when getting variables from $translate for the language string.

Here's the controller for the button with a dropdown. Comments describe things that work and things that don't on several lines.

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
}]);

I'm new but, shouldn't $translate service work in a controller. Presuming that's true, what has to change to get changes to occur. I recently added ng-click to the HTML for the button, but by itself it has had no impact.


回答1:


I began to suspect that the 2d to last line in the OP was not executing. Lines before it executed, lines after it executed, but alerts() around it were odd. So I put that line inside the function, and it worked. Here's the revised, working code.

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
}]);

I remain mystified about why $scope.getCurrentLanguage only works as an anonymous function, and why $scope.ddMenuOptions must be inside the function. But it works, and its very simple.



来源:https://stackoverflow.com/questions/25798947/cant-get-translate-service-from-angular-translate-to-change-dropdown-array-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!