如何使用一个组件来验证其他两个组件?

Igo*_*ets 7 html javascript validation typescript angular

我有三个组件:GalleryAddComponent添加一个新元素,GalleryItemComponent编辑元素,FieldsComponent我想在组件中使用的表单:GalleryAddComponentGalleryItemComponent.所有组件都在里面GalleryComponent.但是当我去组件GalleryAddComponent添加一个新元素时,我得到错误:ERROR TypeError: Cannot read property 'controls' of undefined.也在组件中:GalleryItemComponent.

帮助解决此问题,以便编辑和添加逻辑正常工作.

GalleryAddComponent的模板

<div class="card">
    <div class="card-body">
        <form [formGroup]="angForm" novalidate>
                <app-fields [formGroup]="angForm"></app-fields>
            <div class="form-group but-group">
                <button (click)="addPost(title.value, url.value);  angForm.reset(title.value, url.value)"
                        [disabled]="angForm.pristine || angForm.invalid"
                        class="btn btn-primary">Add
                </button>
                <a routerLink="/" class="btn btn-danger">Back</a>
            </div>
        </form>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

GalleryAddComponent的代码

export class GalleryAddComponent implements OnInit {
    angForm: FormGroup;
    isAdded: boolean = false;
    constructor(private fb: FormBuilder, private galleryService: GalleryService) {}

    ngOnInit() {
        this.angForm = this.fb.group({
            title: ['', Validators.required],
            url: ['', Validators.required]
        });
    }

    addPost(title: string, url: string): void {
        this.galleryService.add(title, url).subscribe(res => {
            this.isAdded = true;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

GalleryItemComponent的模板

    <div class="card" *ngIf="toggleEdit">
        <h4>Edit your post</h4>
        <div class="card-body">
            <form [formGroup]="angForm" novalidate>
                <app-fields [formGroup]="angForm"></app-fields>
                <div class="form-group but-group">
                    <input type="button"
                           (click)="updatePost(title.value, url.value)"
                           [disabled]=" angForm.invalid"
                           class="btn btn-primary" value="Update Post">
                </div>
            </form>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

GalleryItemComponent的代码

export class GalleryItemComponent implements OnInit {
   pic: Picture;
   angForm: FormGroup;
   
    constructor(private route: ActivatedRoute,
    private galleryService: GalleryService, private fb: FormBuilder) {}

    ngOnInit() {
        this.angForm = this.fb.group({
            title: ['', Validators.required],
            url: ['', Validators.required]
        });
        this.showPost();
    }

    showPost(): void {
        this.route.params.subscribe(params => {
            this.galleryService.getPicture(params['id']).subscribe(res => {
                this.pic = res;
                this.angForm.setValue({title: res.title, url: res.url})
            })
        })
    }
    updatePost(title: string, url: string): void {
        this.route.params.subscribe(params => {
            this.galleryService.update(title, url, params['id']).subscribe(res => {
                if (res.id === this.pic.id) {
                    this.pic.title = title;
                    this.pic.url = url;
                } 
            });
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

FieldsComponent的模板

<div [formGroup]="formGroup">
    <div class="form-group">
        <label class="col-md-4">Picture Title</label>
        <input type="text" class="form-control" formControlName="title" minlength="1" #title/>
    </div>
    <div *ngIf="angForm.controls['title'].invalid && (angForm.controls['title'].dirty || angForm.controls['title'].touched)"
         class="alert alert-danger">
        <div *ngIf="angForm.controls['title'].errors.required">
            Title is required.
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4">Picture Address (url)</label>
        <input type="url" class="form-control" formControlName="url" #url pattern="https?://.+"
               title="Include http://"/>
    </div>
    <div *ngIf="angForm.controls['url'].invalid && (angForm.controls['url'].dirty || angForm.controls['url'].touched)"
         class="alert alert-danger">
        Address(url) is required.
        <div *ngIf="angForm.controls['url'].errors.required ">

        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

FieldsComponent的代码

export class FieldsComponent implements OnInit {
@Input() formGroup: FormGroup;

  constructor() {}

  ngOnInit() {}

}
Run Code Online (Sandbox Code Playgroud)

Wro*_*kar 1

您收到此错误是因为您正在引用angForms.controls只有FieldsComponent一个变量:formGroup

只需将angForms模板 HTML 代码替换为formGroup即可解决问题。

<div [formGroup]="formGroup">
  <div class="form-group">
    <label class="col-md-4">Picture Title</label>
    <input type="text" class="form-control" formControlName="title" minlength="1" #title />
  </div>
  <div *ngIf="formGroup.controls['title'].invalid && (formGroup.controls['title'].dirty || formGroup.controls['title'].touched)"
    class="alert alert-danger">
    <div *ngIf="formGroup.controls['title'].errors.required">
      Title is required.
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-4">Picture Address (url)</label>
    <input type="url" class="form-control" formControlName="url" #url pattern="https?://.+" title="Include http://" />
  </div>
  <div *ngIf="formGroup.controls['url'].invalid && (formGroup.controls['url'].dirty || formGroup.controls['url'].touched)"
    class="alert alert-danger">
    Address(url) is required.
    <div *ngIf="formGroup.controls['url'].errors.required ">

    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)