首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度2:在ngFor中对齐/定位项

角度2:在ngFor中对齐/定位项
EN

Stack Overflow用户
提问于 2016-09-27 12:28:17
回答 1查看 591关注 0票数 1

我正在学习Angular2。我正在尝试将数组值绑定到UI。为此,我使用了ngFor和ngSwitch。我想根据ngSwitchCase显示记录。但是现在,根据数组中的id,它正在显示记录。

代码语言:javascript
复制
<div *ngFor="let item of items; let i=index;">    
    <div [ngSwitch]="item.name">
        <div class="form-control" *ngSwitchCase="'months_name'">
            <label>First Field</label>
            <div class="ctrl-wpr">
                <md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
            </div>
        </div>
        <div class="form-control" *ngSwitchCase="'current_medication'">
            <label>Second Field</label>
            <div class="ctrl-wpr">
                <md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
            </div>
        </div>
        <div class="form-control" *ngSwitchCase="'occupation'">
            <label>Third Field</label>
            <div class="ctrl-wpr">
                <md-slide-toggle color="primary" [(ngModel)]="item.value"></md-slide-toggle>
            </div>
        </div>
        <div class="form-control" *ngSwitchCase="'current_medical_problems'">
            <label>First Field</label>
            <div class="ctrl-wpr">
                <md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
            </div>
        </div>
        <div class="form-control" *ngSwitchCase="'husband_medication'">
            <label>Second Field</label>
            <div class="ctrl-wpr">
                <md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value"></md-input>
            </div>
        </div>
        <div class="form-control" *ngSwitchCase="'spouseOccupation'">
            <label>Third Field</label>
            <div class="ctrl-wpr">
                <md-slide-toggle color="primary" [(ngModel)]="item.value"></md-slide-toggle>
            </div>
        </div>
    </div>
</div>

脚本

代码语言:javascript
复制
items: any = [{
    "id": 2952,
    "name": "months_name",
    "value": "400201"
  }, {
    "id": 2964,
    "name": "occupation",
    "value": "Business"
  }, {
    "id": 7236,
    "name": "spouseOccupation",
    "value": "Housewife"
  }, {
    "id": 7244,
    "name": "analysis_report",
    "value": "11"
  }, {
    "id": 7245,
    "name": "husband_medication",
    "value": "No"
  }, {
    "id": 7246,
    "name": "current_medication",
    "value": ""
  }, {
    "id": 7247,
    "name": "current_medical_problems",
    "value": "Heart Problem",
  }]

帮助我如何基于*ngSwitchCase.显示/定位记录

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-27 13:44:11

一种快速的方法是对组件进行排序,在项目和您想要的顺序之间创建一个映射。在某种程度上,您正在复制代码,在组件上执行如下操作:

代码语言:javascript
复制
ngOnInit(){
  this.items.sort((a,b) => this.nameMapping(a.name) - this.nameMapping(b.name));
}

private nameMapping (name) {
  switch (name) {
    case 'months_name':
      return 1;
    case 'current_medication':
      return 2;
    case 'occupation':
      return 3;
    case 'current_medical_problems':
      return 4;
    case 'husband_medication':
      return 5;
    case 'spouseOccupation':
      return 6;
    default:
      return 100;
  }
}

更复杂的解决方案是对组件执行所有逻辑,并在项和视图之间创建适当的映射。就像这样:

代码语言:javascript
复制
ngOnInit(){
  this.items = this.items.map(this.mappingItems);
  this.items.sort((a,b) => a.order - b.order);
}

private mappingItems (item) {
  switch (item.name) {
    case 'months_name':
      item.order = 1;
      item.label = 'First Field';
      item.formType = 'input';
      return item;
    case 'current_medication':
      item.order = 2;
      item.label = 'Second Field';
      item.formType = 'input';
      return item;
    case 'occupation':
      item.order = 3;
      item.label = 'Third Field';
      item.formType = 'slider';
      return item;
    case 'current_medical_problems':
      item.order = 4;
      item.label = 'First Field';
      item.formType = 'input';
      return item;
    case 'husband_medication':
      item.order = 5;
      item.label = 'Second Field';
      item.formType = 'input';
      return item;
    case 'spouseOccupation':
      item.order = 6;
      item.label = 'Third Field';
      item.formType = 'slider';
      return item;
    default:
    item.order = 100;
      return item;
  }
}

那么您的html应该是这样的:

代码语言:javascript
复制
<div *ngFor="let item of items; let i=index;">
    <div class="form-control">
        <label>{{ item.label }}</label>
        <div class="ctrl-wpr">
            <md-input class="ctrl-wpr__ctrl" [(ngModel)]="item.value" *ngIf="item.formType === 'input'"></md-input>
            <md-slide-toggle class="ctrl-wpr__ctrl" [(ngModel)]="item.value" *ngIf="item.formType === 'slider'"></md-slide-toggle>
        </div>
    </div>
</div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39724719

复制
相关文章

相似问题

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