简体   繁体   English

单击按钮时,我想隐藏和显示输入字段

[英]I want to hide and show input fields when click on a button

Page.html页面.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页面.ts

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

It will give formcontrol error like below:它会给出如下的表单控制错误:
在此处输入图片说明

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感谢您的帮助

您不能同时使用 formControl 和 ngModel,只能使用其中之一。

Read through the error screenshot you have posted.通读您发布的错误屏幕截图。 It has explanation.它有解释。 If you want to use both formControlName and ngModel use [ngModeloptions]="{standalone:true}"如果你想同时使用 formControlName 和 ngModel 使用[ngModeloptions]="{standalone:true}"

Remove ngModel from the input tag.从输入标签中删除 ngModel。 You are using both ngModel (Template-driven based) as well as formControlName(Reactive form) on the input.您在输入中同时使用 ngModel(基于模板驱动)和 formControlName(反应形式)。

Or if you want to use it with ngModel then use [ngModelOptions]="{standalone: true}" with it.或者,如果您想将它与 ngModel 一起使用,请使用 [ngModelOptions]="{standalone: true}" 。 It is specified in your screenshot also.它也在您的屏幕截图中指定。

Refer this: https://github.com/angular/angular/pull/10314#issuecomment-242218563参考这个: https : //github.com/angular/angular/pull/10314#issuecomment-242218563

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM