无法获得$从翻译服务角转换为改变ngDropdowns下拉阵列 [英] Can't get $translate service from angular-translate to change dropdown array for ngDropdowns

查看:264
本文介绍了无法获得$从翻译服务角转换为改变ngDropdowns下拉阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角转换正在努力改变我的HTML。我为ngDropdown按钮指令,也有可能改变选择项目的数组。更改语言后,我试图让与translate.use新的/当前的语言()。东西是行不通的。

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.

以下是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控制器。它应该从$翻译并使用该字符串来获得下拉选择数组新的/当前的语言,ddSelections.withLangChoice $ scope.getCurrentLanguage选择时很难codeD,而不是从$获取变量时转换为正确的阵列语言的字符串。

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

我是新的,但在一个控制器不能$翻译服务工作。 presuming这是真的,什么也改变去发生变化。我最近增加了NG-点击到HTML的按钮,但它本身也没有任何影响。

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.

推荐答案

我开始怀疑2D到在OP最后一行没有被执行。行前执行,它执行的行之后,但警报(),它周围是奇怪。所以我把里面的函数那行,和它的工作。这里的修订工作code。

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

我依然困惑不解,为什么$ scope.getCurrentLanguage只能作为一个匿名函数,为什么$ scope.ddMenuOptions必须在函数内部。但它的工作原理,它非常简单。

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.

这篇关于无法获得$从翻译服务角转换为改变ngDropdowns下拉阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆