首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有不一致数据结构的嵌套ngrepeat

具有不一致数据结构的嵌套ngrepeat
EN

Stack Overflow用户
提问于 2015-03-27 10:55:50
回答 1查看 29关注 0票数 0

我正在使用ng-repeat从JSON对象创建一组笔记(div)。卡片的数量由数组中JSON对象的数量决定。Type表示卡片的头部,Content表示卡片的主体。然而,“内容”的内容并不一致。在一些例子中,内容仅仅是一个句子或段落。在另一些情况下,内容是一个数组。当内容是句子时,格式化处理得很好。当内容是一个数组时,我最终以文本格式的实际数组[{"year":"2009", "operation":"caesarean"},{"year":"2010", "operation":"torn meniscus"}]作为卡片的主体。有没有办法在不单独编写每张卡的脚本的情况下实现这一点?

HTML

代码语言:javascript
复制
<section name="patientinfo">
<div  align="center" ng-controller="HistoryCtrl">
<div class="grid-container" ng-repeat="record in historyItems">
    <div class="row" row>
        <div class="col-2" ng-repeat="history in record.History">
            <div class="note" draggable="true">
                <div class="row" id="rowhead3">
                    <div class="col-6" >
                        <span class="note-header">{{history.type}}</span>
                    </div>
                </div>
                <div class="row">
                    <div class="col-6" style="overflow: auto; height:250px;">
                        <div>
                            <span class="note-content">
                                {{history.content}}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</section>

JSON示例(实际的JSON非常庞大)。除了“历史”之外,还有许多其他的条目。另外,这些笔记不是真人写的,所以不用担心。

代码语言:javascript
复制
  "History": [
            {
                "type": "medical",
                "content": [
                    {
                        "condition": "IBS"
                    },
                    {
                        "condition": "Torn Meniscus Right Knee"
                    },
                    {
                        "condition": "Seasonal Allergies"
                    }
                ]
            },            
             {
                "type": "SOCIAL HISTORY",
                "content": "Lives with husband and 3 children. No history of violence, domestic violence, or rape. Feels safe at home and in relationship. ETOH on weekends (socially 4 drinks in one weekend occasionally) and occasionally smokes cigarettes and marijuana. Admits to very rare marijuana on special occasions."
            }]

我最终得到的例子是:http://i.stack.imgur.com/MtuBN.png

EN

回答 1

Stack Overflow用户

发布于 2015-03-27 11:14:07

您应该将数据转换或扩充为一种新的格式,其中每个注释都具有以下结构:

代码语言:javascript
复制
{
    type: "string",
    contentType: "(text|list)",
    content: "string" // OR array of strings
}

为此,您将需要自定义逻辑来将数组中的每个对象转换为单个字符串。

然后只需在contentType属性上使用ng-switch来决定使用哪个标记(例如,如果contentTypelist,则使用<ul> )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29292532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档