【问题标题】:Ionic 2 - Input ‘Go’ field on Keyboard is doing two thingsIonic 2 - 键盘上的输入“Go”字段正在做两件事
【发布时间】:2017-06-08 20:08:11
【问题描述】:

我正在为 iOS 和 Android 构建一个 Ionic 应用程序,到目前为止,我有一个表单,用户可以在其中上传或拍照,然后填写一些其他信息并提交表单。

我的问题:

目前,当用户转到 Input 输入一些额外的文本时,键盘底部有一个 go 按钮。如果用户单击此“开始”按钮,它会尝试提交表单并打开我拥有的 choosePicture() 函数。

当我点击 Go button - it opens the Library

HTML

<ion-item class="file-upload">
    <button  class="btn btn-block" ion-button (click)="choosePhoto()">Choose a Photo</button>
    <span class="sep"></span>
    <button class="btn btn-block" ion-button (click)="takePicture()">Take a Picture</button>
    <div class="uploaded-img">
        <input type="file" value="" formControlName="hazardImage" hidden />
        <span class="img-desc">Please Take or Upload an Image.</span>
        <div class="img-picked" *ngIf="base64Img">
            <img [src]="base64Img" alt="Hazard Picture" />
            <!-- <img src="http://placehold.it/300x300" alt="Test Image" /> -->
        </div>
    </div>
</ion-item>

<small class="small-title">Where is the Hazard Location? (required)</small>
<ion-item class="input-box">
    <ion-input type="text" formControlName="hazardLocation" [class.invalid]="!hazardForm.controls.hazardLocation.valid && (hazardForm.controls.hazardLocation.dirty || submitAttempt)"></ion-input>
</ion-item>

打字稿:

choosePhoto()
{
    this.Camera.getPicture({
        quality: 50,
        destinationType: this.Camera.DestinationType.DATA_URL,
        encodingType: this.Camera.EncodingType.JPEG,
        sourceType: this.Camera.PictureSourceType.PHOTOLIBRARY,
        allowEdit: true
    }).then((ImageData) => {
        // console.log(ImageData);
        this.base64Img = ImageData;
        this.hazardForm.patchValue({
            hazardImage: this.base64Img
        });
    }, (err) => {
        console.log(err);
    })
}

我不明白为什么Go按钮打开了选择照片的功能:/

【问题讨论】:

标签: ionic-framework ionic2


【解决方案1】:

您可以修改“开始”键盘按钮的行为:

@ViewChild('input-box') inputElm: ElementRef;
@HostListener('keyup', ['$event'])
keyEvent(e) {

if(!e.srcElement) return; //browser fix
var code = e.keyCode || e.which;
if (code === 13) {
  if (e.srcElement.tagName === "INPUT") {
    e.preventDefault();
    //handle other behaviours
  }
}
};

【讨论】:

  • 完全忘了preventDefault(),不错。
【解决方案2】:

您已将“危险位置”的&lt;input&gt; 定义为表单,因为存在formControlName="hazardLocation" 属性。这使得Go 或表单提交按钮出现在移动键盘中。

相反,您应该将其作为普通输入字段,像这样

<input [(ngModel)]="hazardLocation">

【讨论】:

  • 嗯...我不介意formControlName,因为我需要捕获数据并发送到我的服务器-我的问题是键盘上的Go按钮正在打开我的照片库-就像我在附加的链接中显示的那样:/ 如果可以的话,我希望将“开始”按钮更改为“完成”按钮,但如果不能,我宁愿它不打开照片库
  • 请尝试我向您展示的更改。它仍然将输入与hazardLocation 绑定并自动更新它以供您捕获并发送到服务器。这是正确的做法。
  • 我尝试了您展示的内容,但仍然不好,“开始”按钮正在打开打开用户库的选择照片
猜你喜欢
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 2019-01-30
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
相关资源
最近更新 更多