如何在angular2中以窗体数组显示嵌套数组数据?

时间:2017-04-08 08:28:04

标签: json angular angular2-forms

我在angular2项目中实现了FormGroup来渲染表单,我使用formArray来获取嵌套数组数据

form.component.html

<div class="col-md-12" formArrayName="social_profiles">
  <div *ngFor="let social of resumeForm.controls.social_profiles.controls; let i=index" class="panel panel-default m-t-10">
    <div class="panel-heading" style="min-height: 30px;">
      <span class="glyphicon glyphicon-remove pull-right" *ngIf="resumeForm.controls.social_profiles.controls.length > 1" (click)="removeSocialProfiles(i)"></span>
    </div>
    <div class="panel-body" [formGroupName]="i">
      <social-profile [group]="resumeForm.controls.social_profiles.controls[i]"></social-profile>
    </div>
  </div>
</div>

form.component.ts

export class FormComponent implements OnInit {
    public resumeForm: FormGroup;

    constructor(private formBuilder: FormBuilder) {}

    ngOnInit() {
        this.resumeForm = this.formBuilder.group({
            name: ['', Validators.required],
            label: ['',],
            email: [''],
            phone: [''],  
            social_profiles: this.formBuilder.array([])
        });

        this.addSocialProfiles();
    }

    initSocialProfiles() {
        return this.formBuilder.group({
            network: [''],
            url: ['']
        });
    }

    addSocialProfiles() {
        const control = <FormArray>this.resumeForm.controls['social_profiles'];
        const addrCtrl = this.initSocialProfiles();        
        control.push(addrCtrl);      
    }

    removeSocialProfiles(i: number) {
        const control = <FormArray>this.resumeForm.controls['social_profiles'];
        control.removeAt(i);
    }
}

其中 social-profile 是数组的子表单

社交profile.component.html

<div [formGroup]="socialForm">
    <div class="col-md-6">
        <input type="text" class="form-control" placeholder="Enter Network" formControlName="network">
    </div>
    <div class="col-md-6">
        <input type="text" class="form-control" placeholder="Enter Network URL" formControlName="url">
    </div>
</div>

社交profile.component.ts

export class SocialProfileComponent {
    @Input('group')
    public socialForm: FormGroup;
}

从服务回调中获取JSON

{
    "basics_info": {
        "name": "Joh Doe",
        "label": "Programmer",
        "Social_profiles": [{
            "network": "Twitter",
            "url": "https://www.twitter.com/kumarrishikesh12"
        }, {
            "network": "Facebook",
            "url": "https://www.facebook.com/kumarrishikesh12"
        }]
    }
}

添加数据表单工作正常但如何在页面init上的编辑时间显示表单中现有的json嵌套数组(从api获取)?如下图所示

enter image description here

1 个答案:

答案 0 :(得分:4)

这里假设你已经从对象booked中提取了内容,即:

并将JSON分配给变量basics_info,以便最终得到data的内容:

data

然后让我们修补这些值。您可以在收到数据后收到JSON(或构建表单)后修补值。在这里,我在构建表单后修补值。

下面我想澄清一下调用{ "name": "Joh Doe", "label": "Programmer", "Social_profiles": [{ "network": "Twitter", "url": "https://www.twitter.com/kumarrishikesh12" }, { "network": "Facebook", "url": "https://www.facebook.com/kumarrishikesh12" }] } 中的另一个方法,它实际上设置了值。

patchValues

孩子应该很好地抓住这些变化。这里有一个演示,变量是不同的,因为我使用了现有的表格。但逻辑完全一样。

Plunker

相关问题