添加输入字段Angular

时间:2017-09-10 19:16:03

标签: javascript angular angular-forms

我尝试创建一个在Angular中使用TypeScript添加输入字段的函数。看看这个屏幕。

enter image description here

我在JSON中获得正确的字段视图,但在实际屏幕中没有。我不知道如何改变它。我尝试了很多解决方案,但没有成功。

app.component.html

<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <div class="margin-20">
        <h4>Add customer</h4>
      </div>
      <form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm)">
        <div class="form-group">
          <label>Name</label>
          <input type="text" class="form-control" formControlName="name">
          <small *ngIf="!myForm.controls.name.valid" class="text-danger">
            Name is required (minimum 5 characters).
          </small>
        </div>
        <!--addresses-->
        <div formArrayName="addresses">
          <div *ngFor="let address of myForm.controls.addresses.controls; let i=index" class="panel panel-default">
            <div class="panel-heading">
              <span>Address {{i + 1}}</span>
              <span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.addresses.controls.length > 1" (click)="removeAddress(i)"></span>
            </div>
            <div class="panel-body" [formGroupName]="i">
              <div class="form-group col-xs-6">
                <label>street</label>
                <input type="text" class="form-control" formControlName="street">
                <small [hidden]="myForm.controls.addresses.controls[i].controls.street.valid" class="text-danger">
                  Street is required
                </small>
              </div>
              <div class="form-group col-xs-6">
                <label>postcode</label>
                <input type="text" class="form-control" formControlName="postcode">
              </div>
              <div *ngFor="let answer of myForm.controls.answers.controls; let 
    i=index" class="panel panel-default">
                <div class="form-group col-xs-6">
                  <label>answer</label>
                  <input type="text" class="form-control" 
     formControlName="answer">
                </div>
              </div>

              <div class="margin-20">
                <a (click)="addAnswers()" style="cursor: default">
                  Add another answer +
                </a>
              </div>

            </div>
          </div>
        </div>

        <div class="margin-20">
          <a (click)="addAddress()" style="cursor: default">
            Add another address +
          </a>
        </div>

        <div class="margin-20">
          <a (click)="initA()" style="cursor: default">
            Add another address +
          </a>
        </div>




        <div class="margin-20">
          <button type="submit" class="btn btn-primary pull-right" 
     [disabled]="!myForm.valid">Submit</button>
        </div>
        <div class="clearfix"></div>

        <div class="margin-20">
          <div>myForm details:-</div>
          <pre>Is myForm valid?: <br>{{myForm.valid | json}}</pre>
          <pre>form value: <br>{{myForm.value | json}}</pre>
        </div>
      </form>
    </div>
  </div>
  </div>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormArray,  FormBuilder, Validators } from '@angular/forms';
import { Customer } from './customer.interface';

@Component({
  moduleId: module.id,
  selector: 'my-app',
  templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {
    public myForm: FormGroup;

    constructor(private _fb: FormBuilder) { }

    ngOnInit() {
        this.myForm = this._fb.group({
            name: ['', [Validators.required, Validators.minLength(5)]],
            addresses: this._fb.array([
                this.initAddress(),
            ]),
          answers: this._fb.array([
            this.initAnswers(),
          ])
        });
    }


  initA() {
    this.myForm = this._fb.group({
      name: ['', [Validators.required, Validators.minLength(5)]],
      addresses: this._fb.array([
        this.initAddress(),
      ]),
      answers: this._fb.array([
        this.initAnswers(),
      ])
    });
  }


    initAddress() {
        return this._fb.group({
            street: ['', Validators.required],
            postcode: [''],
            answer: ['']
        });
    }

    addAddress() {
        const control = <FormArray>this.myForm.controls['addresses'];
        control.push(this.initAddress());
    }

    removeAddress(i: number) {
        const control = <FormArray>this.myForm.controls['addresses'];
        control.removeAt(i);
    }

  initAnswers() {
    return this._fb.group({
      answer: ['']
    });
  }

     addAnswers() {
    const control = <FormArray>this.myForm.controls['answers'];
    control.push(this.initAnswers());
  }

  removeAnswers(i: number) {
    const control = <FormArray>this.myForm.controls['answers'];
    control.removeAt(i);
  }

    save(model: Customer) {
        // call API to save
        // ...
        console.log(model);
    }
}

0 个答案:

没有答案