Angular 2 nested forms with child components and validation

后端 未结 6 1256
醉话见心
醉话见心 2021-02-05 13:16

I\'m trying achieve a nested form with validation in Angular 2, I\'ve seen posts and followed the documentation but I\'m really struggling, hope you can point me in the right di

6条回答
  •  深忆病人
    2021-02-05 13:30

    To get a reference to the parent form simply use this (maybe not available in Angular 2. I've tested it with Angular 6):

    TS

    import {
       FormGroup,
       ControlContainer,
       FormGroupDirective,
    } from "@angular/forms";
    
    @Component({
      selector: "app-leveltwo",
      templateUrl: "./leveltwo.component.html",
      styleUrls: ["./leveltwo.component.sass"],
      viewProviders: [
        {
          provide: ControlContainer,
          useExisting: FormGroupDirective
        }
      ]
    })
    export class NestedLevelComponent implements OnInit {
      //form: FormGroup;
    
      constructor(private parent: FormGroupDirective) {
         //this.form = form;
      }
    }
    

    HTML

    
    

提交回复
热议问题