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); }