2017-01-02 34 views
3

我有以下输入字段,当点击图标时,输入字段应该切换到另一个输入ID和标签名称。再次点击时,应该再次改回。目前我已经解决了2个独立的* .html文件并在它们之间切换。我现在正在寻找更智能的解决方案。用可点击的图标切换输入字段

<ion-content> 
     <div class="item-input-wrapper"> 
     <ion-item> 
     <ion-label stacked>Your name</ion-label> 
     <ion-input id="name" type="text"></ion-input> 
     <ion-icon ion-button (click)="changeToStudentNumber()" name="ios-switch" clear item-right></ion-icon> 
     </ion-item> 
     </div> 
    </ion-content> 

二HTML如下:

<ion-content> 
     <div class="item-input-wrapper"> 
     <ion-item> 
     <ion-label stacked>Your student number</ion-label> 
     <ion-input id="studentnumber" type="text"></ion-input> 
     <ion-icon ion-button (click)="changeToName()" name="ios-switch" clear item-right></ion-icon> 
     </ion-item> 
     </div> 
    </ion-content> 

回答

1

您是否尝试过使用ngIf?

<ion-content> 
    <div class="item-input-wrapper"> 
    <ion-item *ngIf="useName"> 
    <ion-label stacked>Your name</ion-label> 
    <ion-input #nameInput id="name" type="text"></ion-input> 
    <ion-icon ion-button (click)="toggleUseName(nameInput)" name="ios-switch" clear item-right></ion-icon> 
    </ion-item> 
    <ion-item *ngIf="!useName"> 
    <ion-label stacked>Your student number</ion-label> 
    <ion-input #numberInput id="studentnumber" type="text"></ion-input> 
    <ion-icon ion-button (click)="toggleUseName(numberInput)" name="ios-switch" clear item-right></ion-icon> 
    </ion-item>  
    </div> 
</ion-content> 

toggleUseName()然后只需扳动控制器这样的useName财产;

toggleUserName(inputToFocus) { 
    this.useName = !this.useName; 
    inputToFocus.setFocus(); 
} 
+0

谢谢效果很好。我这样做:'toggleUseName() { if(this.useName){this.useName = false;其他{ this.useName = true; } }' - 你会推荐吗? –

+0

这会工作,但可以做得更简单。我编辑过这个帖子以包含一个'toggleUseName()'函数的小样本。 – Robba

+0

对不起,我还有一个问题。切换后输入栏是否自动对焦也可以吗? –