javascript - ionic 获取文本框值

标签 javascript ionic-framework ionic2

您好,我是 ionic 的新手,我有以下 html 和 js 代码。我试图在单击按钮时获取用户在文本框中输入的值。 (如果可能的话,我想将它存储到一个对象或本地缓存中,以便它可以在同一个 session 中使用)

<ion-navbar *navbar hideBackButton>
     <ion-title>Welcome</ion-title>
</ion-navbar>

<ion-content>

<ion-list>

    <ion-item>
        <ion-label floating>Please enter a nickname</ion-label>
        <ion-input type="text" value=""></ion-input>
    </ion-item>

</ion-list>

<div padding>
    <button (click)="login()" primary block>Continue</button>
</div>

接下来是我的 .js 代码

 import {Component} from "@angular/core";
 import {MenuController, NavController, Alert} from "ionic-angular";
 import {Index} from "../index/index";

 @Component({
    templateUrl: 'build/pages/login/login.html'
 })
 export class Login {
    static get parameters() {
        return [[MenuController], [NavController]];
    }

 constructor(menu, nav) {
        this.menu = menu;
        this.menu.swipeEnable(false);

        this.nav = nav;
    }

    login() {
        let alert = Alert.create({
        title:      'You have entered',
        message:    'Hello',
        buttons:    [
    {
      text: 'Cancel',
      handler: () => {
        console.log('Cancel clicked');
      }
    },
    {
      text: 'Ok',
      handler: () => {
        console.log('Ok clicked');

                    console.log(getElementById('nickname'));

                    // this.menu.swipeEnable(true);
                    // this.nav.pop(Login);
                    // this.nav.setRoot(Index);
      }
    }
  ]
    });

    this.nav.present(alert);
}
 }

最佳答案

Ionic 与 angular 一起工作,angular 以两种方式绑定(bind)作为其核心原则。有很多方法可以实现这一点,但一种方法是设置 html 项目的模型。 因此,如果您将输入设置为具有 ng 模型

<ion-input type="text" value="" [(ngModel)]="inputName"></ion-input>

然后在你的 Controller (类)中你将拥有

this.inputName;

它将保留来自 html 的值更改。

关于javascript - ionic 获取文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672194/

相关文章:

Android Ionic Cordova - 错误 : Failed to get absolute path to installed module

javascript - 手动抛出错误ionic 2

javascript - 自动图像悬停效果

javascript - 从客户端 JavaScript 打开扩展库对话框

javascript - 在 map 完成后返回一个 object.map() 内部

android - 无法使用 ionic 项目创建 apk

javascript - 未捕获( promise ): TypeError: Cannot read property 'create' of undefined (ionic 3. 9,Angularjs 5.0.3)

javascript - Konva.js 的上下文菜单

angularjs - 如何使用 ionic 实现评级星级

cordova - Ionic/Angular 2 组件事件发射器不更新 View