Check if a form is valid from a parent component using Angular 4

前端 未结 1 337
臣服心动
臣服心动 2020-12-16 05:38

I am trying to check the validity of a form placed on a child component from a parent component. The best scenario would be to disable a button on the parent component if th

相关标签:
1条回答
  • 2020-12-16 06:02

    Since Angular applies ngForm to all form elements implicitly and you can reference any component/directive in the component template by component/directive class reference, you don't have to read ElementRef and don't need the template reference #candiateForm, you can access the form directly by the directive class reference ngForm:

    export class RightPanelComponent implements OnChanges , OnInit{
        @ViewChild(NgForm) form;
    
        checkFormValidity():boolean {        
            return this.form.valid;
        }
    

    And also you can access the form directly from the parent component:

    export class CandidateVerificationComponent implements OnChanges, OnInit {
        @ViewChild(RightPanelComponent) rightPanelPointer: RightPanelComponent;
    
        saveAndFinish() {
            if (this.rightPanelPointer.form.valid)
    
    0 讨论(0)
提交回复
热议问题