简体   繁体   English

如何使用ng-Repeat对数组对象进行角度转换?

[英]How to us ng-Repeat for angular translate for array objects?

I am currently working on an Angular project with the plugin angular translate from Pascal Precht. 我目前正在使用Angular项目进行工作,该插件使用了Pascal Precht的angular translation插件。 I have an FAQ page which has a tabs for answer. 我有一个FAQ页面,其中有一个选项卡。 What I want to achieve is to have a dynamic values for questions. 我想要实现的是具有动态的问题价值。 So that I will just have a loop for those questions and those questions will be in a translation file as Json array. 这样我就可以对这些问题进行循环,这些问题将作为Json数组存储在翻译文件中。 here is my sample json file. 这是我的示例json文件。

"FAQS_QUESTION"  : [
   {
     "question" : "Question 1?",
     "answer"   : "Answer 1 "
   },
   {
     "question" : "Question 2?",
     "answer"   : "Answer 2" 
   },
   {
     "question" : "Question 3?",
     "answer"   : "Answer 3" 
   }
]

I tried the code below but it is not working. 我尝试了下面的代码,但无法正常工作。 I get a "angular.js:14525 Translation for FAQS_QUESTION doesn't exist" 我收到“ angular.js:14525 FAQS_QUESTION的翻译不存在”

ul( ng-repeat="thumbnail in 'FAQS_QUESTION' | translate ")
   li 
       | {{thumbnail.question}}
       | {{thumbnail.answer}}

I Also tried loading it in my Controller with no luck 我也尝试将其加载到我的控制器中没有运气

$translate('FAQS_QUESTION').then(function (translations) {
    console.log(translations.questions);
}, function (translationIds) {
    console.log(translationIds);
});

I found a workaround and the implementation is like this: 我找到了一种解决方法,实现是这样的:

ul(ng-init="count = ('FAQS_QUESTION_COUNT' | translate)")
  li(ng-repeat="item in getQuestionCount(count) track by $index")
     p(translate="FAQS_QUESTION.{{$index}}.question")
     p(translate="FAQS_QUESTION.{{$index}}.answer")

It solves my problem. 它解决了我的问题。 But it will be a tedious for maintenance to indicate the count for each Iteration. 但是,指出每个迭代的计数对于维护来说将是一件繁琐的工作。 Since we are planning to have a section for each quetions 由于我们计划为每个风琴设置一个部分

Can anyone help me for the workaround for this one? 有人可以帮我解决这个问题吗? I am not sure I appreciate all your help. 我不确定您是否能感谢您的帮助。 Thanks 谢谢

In this snippet 在这个片段中

ul( ng-repeat="thumbnail in 'FAQS_QUESTION' | translate ")
   li 
       | {{thumbnail.question}}
       | {{thumbnail.answer}}

your value 'FAQS_QUESTION' is just a plain string, so you're not referencing your data. 您的值'FAQS_QUESTION'只是一个纯字符串,因此您没有引用数据。 Also, the translate -filter doesn't work with collections i believe. 另外,我相信translate -filter不适用于集合。

Instead, try doing something like this: 相反,请尝试执行以下操作:

ul( ng-repeat="thumbnail in FAQS_QUESTION")
   li 
       | {{thumbnail.question | translate}}
       | {{thumbnail.answer | translate}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM