如何获取离子的输入文本值

Jan*_*uka 13 html ionic-framework angular

我正在尝试获取input文本值并将其存储在名为input中的变量中ionic。但是我尝试并失败了。谁能告诉我我做错了什么吗?

这是我的 HTML

  <ion-content>
  <ion-list>
    <ion-item> 
        <ion-label stacked>display</ion-label>
      <ion-input type="text" text-right id="input" ></ion-input>
    </ion-item>    
  </ion-list>
  </ion-content>
Run Code Online (Sandbox Code Playgroud)

这是我home.ts的离子

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {
    var input=document.getElementById('input').nodeValue;
    document.getElementById('seven').innerHTML=input;
  }

}
Run Code Online (Sandbox Code Playgroud)

谁能帮帮我吗?

Saj*_*ran 15

实际上,您似乎使用的是angular而不是angularjs,请使用[(ngModel)]

 <ion-input type="text" [(ngModel)]="name" text-right id="input" ></ion-input>
Run Code Online (Sandbox Code Playgroud)

在组件内部,

name:string;

因此,只要您需要该值,就可以使用。

console.log(this.name);


Ofi*_*r G 7

<ion-content>
  <ion-list>
    <ion-item> 
      <ion-label stacked>display</ion-label>
      <ion-input type="text" text-right id="input" [(ngModel)]="inputValue"></ion-input>
    </ion-item>    
  </ion-list>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

// ===

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    inputValue: string = "";
    constructor(public navCtrl: NavController) {}

    someFunction() {
        // here you can use the 'this.inputValue' and get the value of the ion-input 
    }

}
Run Code Online (Sandbox Code Playgroud)

我们使用两种方式将ion-input的值与类成员inputValue绑定,

关于ngModel

如果您需要访问输入值,请检查inputValue的值。

在这里您可以看到我在StackBlitz上写的一个示例

双向绑定是属性绑定和事件绑定的组合,因为它是从表示层到组件以及从组件到表示层的数据/值的连续同步。

由于这是一种双向绑定,因此我们必须使用两个方括号-[()]。ngModel也是一种指令,用于双向绑定数据。