无法从angular-translate获得$ translate服务以更改ngDropdowns的下拉数组 [英] Can't get $translate service from angular-translate to change dropdown array for ngDropdowns

查看:34
本文介绍了无法从angular-translate获得$ translate服务以更改ngDropdowns的下拉数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'|翻译}}</button></div> 

这是该postButton的控制器.它应该从$ translate获取新/当前语言,并使用该字符串获取下拉选择数组ddSelections.withLangChoice $ scope.getCurrentLanguage在进行硬编码时选择正确的数组,但从$ translate获取用于语言字符串的变量时则选择正确的数组./p>

这是带有下拉菜单的按钮的控制器.注释描述了行之有效的事情,而不是行不通的事情.

  residenceApp.controller('postButtonController2',['$ translate','$ scope','changePostDdFactory',函数($ translate,$ scope,ddSelections){//ddSelections是changePostDdFactory返回的函数的结果对象$ scope.getCurrentLanguage = function($ translate){alert('我在postButtonController2中');//触发按钮点击和页面加载alert('我在postButtonController2中'+ $ translate.use());//不会在发布按钮点击时触发.在页面加载时触发$ scope.langKey = $ translate.use();//仅在页面加载时获取$ translate中的langKey$ translate.refresh($ scope.langKey);//应该刷新翻译,但是什么也没有alert('来自postButtonController2-currentLanguage是'+''+ $ translate.use());//不会在按钮单击时触发.在页面加载时触发//返回'es';//硬编码可在页面加载&将ddSelections.withLangChoice设置为es数组返回$ translate.use();//设置页面加载时ddSelections的语言,不更改语言};$ scope.ddMenuOptions = ddSelections.withLangChoice($ scope.getCurrentLanguage($ translate));//可以在页面加载时工作,不会发生语言更改$ scope.ddMenuSelected = {};//工作代码以观察用户选择的变化}]); 

我是新手,但不应该在控制器中$ translate服务工作.假设是真的,必须进行哪些更改才能使更改发生.我最近将ng-click添加到按钮的HTML中,但是它本身并没有影响.

解决方案

我开始怀疑OP中最后一行的2d没有执行.执行前的行,执行后的行,但周围的alert()很奇怪.因此,我将该行放入函数中,它起作用了.这是经过修改的有效代码.

  residenceApp.controller('postButtonController2',['$ translate','$ scope','changePostDdFactory',函数($ translate,$ scope,ddSelections){var ddSelections;//ddSelections是changePostDdFactory返回的函数的结果对象$ scope.getCurrentLanguage = function(){$ scope.langKey = $ translate.use();//使用()作为吸气剂$ scope.ddMenuOptions = ddSelections.withLangChoice($ scope.langKey);返回;};$ scope.ddMenuSelected = {};//代码以监视用户选择的更改}]); 

我仍然对为什么$ scope.getCurrentLanguage仅用作​​匿名函数以及为什么$ scope.ddMenuOptions必须在函数内部感到迷惑.但这确实有效,而且非常简单.

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.

解决方案

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.

这篇关于无法从angular-translate获得$ translate服务以更改ngDropdowns的下拉数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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