-3

Page.html

             <form [formGroup]="myForm">
        <ion-button [(ngModel)]="isActive"(click)="onClick()" >Add</ion-button>



           <ion-item>
              <ion-label position="floating">First Name</ion-label>
        <ion-input type="text" [(ngModel)]="firstname" formControlName="firstname" ></ion-input>
         </ion-item>

        <ion-item class="item_country" >
          <ion-label position="floating">Date Of Birth</ion-label>
       <ion-datetime formControlName="date"  [(ngModel)]="date" displayFormat="DD/MMM/YYYY"> 
        </ion-datetime>
         </ion-item>

         <ion-item>
          <ion-label position="floating">Passport number</ion-label>
                <ion-input type="text" formControlName="passport"  [(ngModel)]="passport" > 
        </ion-input>
          </ion-item>

          <div *ngIf="isActive">


           <ion-item>

        <ion-label position="floating">First Name</ion-label>
             <ion-input type="text" [(ngModel)]="firstname1" formControlName="firstname1" > 
      </ion-input>

        </ion-item>



          <ion-item class="item_country" >
         <ion-label position="floating">Date Of Birth</ion-label>
        <ion-datetime formControlName="date1"  [(ngModel)]="date1"displayFormat="DD/MMM/YYYY"> 
        </ion-datetime>
         </ion-item>

            <ion-item>
             <ion-label position="floating">Passport number</ion-label>
               <ion-input type="text" formControlName="passport1"  [(ngModel)]="passport1" > 
               </ion-input>
              </ion-item>

            </div>

page.ts

                onClick(){
                this.isActive = !this.isActive;
               }

It will give formcontrol error like below:
enter image description here

I can't understand where am going wrong please help me. I want to show input fields when I click on a button and also hide when I click on it. but give error please help for this. Thank you for your help

Mahak Garg
  • 53
  • 5

3 Answers3

0

You can't use both formControl and ngModel, just use one of them.

0

Read through the error screenshot you have posted. It has explanation. If you want to use both formControlName and ngModel use [ngModeloptions]="{standalone:true}"

Shubham Bhokare
  • 116
  • 1
  • 2
  • 7
  • but in my other forms i used same formConrolName and ngModel together and it will work nicely – Mahak Garg Jan 24 '20 at 07:32
  • Have a look here - (https://stackoverflow.com/questions/39126638/ngmodel-cannot-be-used-to-register-form-controls-with-a-parent-formgroup-directi) – Shubham Bhokare Jan 24 '20 at 07:35
0

Remove ngModel from the input tag. You are using both ngModel (Template-driven based) as well as formControlName(Reactive form) on the input.

Or if you want to use it with ngModel then use [ngModelOptions]="{standalone: true}" with it. It is specified in your screenshot also.

Refer this: https://github.com/angular/angular/pull/10314#issuecomment-242218563