角平移选项 [英] angular-translate ad ng-options

查看:74
本文介绍了角平移选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻译选择下拉列表,但我不喜欢这样做的方式,因为它很麻烦,并且绕过整个angular-translate框架.

I'm trying to translate a select drop-down and I don't like the way I'm doing it, because it's cumbersome and it bypasses the whole angular-translate framework.

语言环境数据看起来像{"lang":{"label":"text","select":{"k1":"var1","k2":"var2"}}}}在控制器范围内的"select"成员中,我可以在select的ng-options中编写类似"k as v for scopedvar中的(k,v)".

The locale data looks like {"lang": {"label": "text", "select": {"k1": "var1", "k2": "var2"}}} and if I plonk the "select" member in the controller scope, I can write something like "k as v for (k,v) in scopedvar" in the ng-options of the select.

基本上,我想翻译以进行语言解析,然后避开障碍并返回本地化地图以供选择.如果有道理,则类似于:将'选择'|翻译中的(k,v)的k表示为v,但当然没有.

Basically I'd like translate to do the language resolution and then get out of the way and return the map of localizations for my options. If it made sense, something like: "k as v for (k, v) in 'select' | translate", but of course it doesn't.

以前有人遇到(并解决)此问题吗?

Has anyone faced (and solved) this issue before?

TIA, 爱德华多

推荐答案

我没有完全理解您要实现的目标,但是我将放置一些代码,这些代码可以很好地重新加载| |选项翻译.翻译过滤器.

I didn't completely get what you're trying to achieve, but I'll put some code that works fine for reloading the options translations with the | translate filter.

假设您将此json作为翻译的键/值:

Assuming you have this json as key/values for your translations:

var english = {"lang": {
                 "label": "text", 
                 "select": {
                    "k1": "var1", 
                    "k2": "var2"
                 }}
              };

您的控制器会创建一个如下所示的选项列表:

And your controller creates a list of options like this:

$scope.optionsList = [
    {val: 'var1', translationKey: 'lang.select.k1'},
    {val: 'var2', translationKey: 'lang.select.k2'}
];

您应该很好地将翻译过滤器插入ng-options表达式中的选项标签之后:

You should be good to go inserting the translate filter after the option label in the ng-options expression:

<select ng-model="selectedOpt" 
        ng-options="opt.val as opt.translationKey | translate for opt in optionsList">
</select>

希望有帮助!

这篇关于角平移选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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