【问题标题】:ionic 3 get text from ion-textarea after clicking a buttonionic 3 单击按钮后从 ion-textarea 获取文本
【发布时间】:2018-05-20 15:43:15
【问题描述】:

我有一个ion-textarea,用户可以在其中编写一些内容,我想在单击按钮后获取此文本。
这是html:

<ion-row>
    <ion-item>
        <ion-textarea [(ngModel)]="myInput" (ionInput)="getItems($event)" placeholder="New Info"></ion-textarea>
    </ion-item>
</ion-row>

<button ion-button class="card-button" color="secondary" 
 (click)="addInfo()"> <ion-icon name="add-circle" class="icona-bottone"></ion-
 icon>Add Info</button>

我尝试在我的 .ts 文件中执行此操作:

getItems(textarea) {
    // set q to the value of the textarea
    var q = textarea.srcElement.value;
    this.textSearch = q;
}
addInfo(){
    console.log("You wrote " + this.textSearch)
}

但它会打印“你写了undefined”。将文本作为字符串获取并使用它的正确方法是什么?

【问题讨论】:

  • 尝试在 getItems() 方法中使用 console.log(textarea) 并查看值
  • 它不打印任何东西
  • 那么你的 getItems() 方法甚至不会被调用。尝试在 addInfo() 中使用 ngModel "myInput" 而不是 this.textSearch

标签: angular typescript ionic-framework textarea ionic3


【解决方案1】:

由于您使用了 2-way 数据绑定,您可以如下所示进行操作。

.html

<ion-row>
    <ion-item>
        <ion-textarea [(ngModel)]="myInput" placeholder="New Info"></ion-textarea>
    </ion-item>
</ion-row>

.ts

console.log(this.myInput);//this is your textarea value

【讨论】:

    猜你喜欢
    • 2018-03-02
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2016-05-10
    • 2015-09-24
    • 2019-06-27
    • 2017-09-05
    相关资源
    最近更新 更多