【问题标题】:Switch Inputfield with a clickable Icon使用可点击图标切换输入字段
【发布时间】:2017-01-02 17:03:04
【问题描述】:

我有如下输入框,当点击图标时,输入框应该切换到另一个输入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>

【问题讨论】:

    标签: angular ionic-framework ionic2


    【解决方案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();
    }
    

    【讨论】:

    • 谢谢,效果很好。我是这样做的:toggleUseName() { if (this.useName) { this.useName = false; } else { this.useName = true; } } - 你会推荐吗?
    • 这可行,但可以做得更简单。我已经编辑了帖子以包含toggleUseName() 函数的小样本。
    • 抱歉,我还有一个问题。切换输入字段后是否也可以自动对焦?
    • 我已经编辑了答案,我很确定它会在输入和 html 中toggleUseName() 调用的参数上做到这一点(注意#...),以及作为 toggleUseName() 函数的更改实现。
    • 这对我不起作用,切换后焦点丢失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2016-11-22
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多