angular 过滤器正在返回一个对象。如何避免这种情况?

angular filter is returning an object. How to avoid that?

我有一个 JSON 文件,它的格式如下所示。有些值只是字符串,而其他值是具有键值对的对象。

[{ "question": "How many names do you have?", "answer": "1) Mark, 2) Ram, 3) Sam, 6) Paul, 7) Bob" }, { "question": "What are your habits?", "answer": [{ "id": 1, "value": "Coding" }, { "id": 2, "value": "Gaming" }, { "id": 3, "value": "Sleeping" }, { "id": 4, "value": "Enjoying good food" }] }, { "question": "What are you like?", "answer": "I'm a simple person who minds my own business." }, { "question": "What habit do you like most?", "answer": "I like coding the most." }]

我在 html 上的输出是 as shown here (click to see image),这正是我想要的。我使用嵌套的 ng-repeat 来显示作为对象的正确值。
但是当我过滤这些结果时,显示的输出是一个 json 对象而不是一个简单的列表,这是我不想要的。
过滤时我在这里犯的错误在哪里,我该如何避免?

请帮忙。 Full working code here

这是因为您的问题索引在您过滤时发生了变化。不要使用 counselingJson[$index].answer,而是使用 qAndA.answer。工作 plunkr:

https://plnkr.co/edit/cTCzUyS89b7KAh3RnAlM?p=preview

我为您的代码找到了解决方案,请在您的 json 数组值后添加分号;

 {
        "question": "What habit do you like most?",
        "answer": "I like coding the most."
    }];

对于条件,您应该检查是否如下所示:

<div ng-if="isString(qAndA.answer)">
    {{qAndA.answer}}
</div>
<div ng-if="isArray(qAndA.answer)">
   <ul ng-repeat="a in counselingJson[$index].answer">
      <li><strong>{{a.id}})</strong> {{a.value}}</li>
   </ul>
</div>