美文网首页
Ionic3在ts中获取html中值的方法

Ionic3在ts中获取html中值的方法

作者: my木子 | 来源:发表于2018-09-10 11:28 被阅读0次

参数传递法

例子:获取input框内容

这里有个独特的地方,直接在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文件中

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

双向绑定法

需要在ts中定义对应的变量

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

相关文章

网友评论

      本文标题:Ionic3在ts中获取html中值的方法

      本文链接:https://www.haomeiwen.com/subject/bsxsgftx.html