ionic獲取表單input的值的兩種方法


1.參數傳遞法

直接在input處使用 #定義參數的name值,注意在ts中參數的類型

html頁面:

<ion-input type="text" placeholder="請輸入賬號" #username></ion-input>
<ion-input type="password" placeholder="請輸入密碼" #password></ion-input>
<button (click)="login(username, password)">登錄</button>

ts文件中(HTMLInputElement類型)

login(username: HTMLInputElement, password: HTMLInputElement) {
    console.log(username.value)
    console.log(password.value)
  }

2.雙向數據綁定

html頁面:

<ion-input type="text" placeholder="請輸入賬號" [(ngModel)]="username"></ion-input>
<ion-input type="password" placeholder="請輸入密碼" [(ngModel)]="password"></ion-input>
<button (click)="login()">登錄</button>

ts文件中:

username: string;
password: string;
login() {
 console.log(this.username);
 console.log(this.password);
}        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM