how to set default value in ionic 2 input?

2019-09-21 11:43发布

问题:

I want to set a default value in ionic input .. please help

address.html file code

<ion-item>
<ion-label floating>{{'address.email'|translate}}</ion-label>
        <ion-input type="text" formControlName="user_email"></ion-input>
    </ion-item>

default mail like example@gmail.com

adderss.ts

    ionViewDidEnter() {
    if (this.isCache) this.getData();
    else this.isCache = true;
}
getData() {
    this.storageMul.get(['login', 'useBilling', 'user']).then(val => {
        if (val['login']) this.login = val['login'];
        if (val['useBilling'] == false) this.useBilling = false;
        else this.useBilling = true;
        if (val['user']) {
            this.data = val['user'];

        }
        this.reset();
    });
}
reset() {
    this.formAddress.patchValue({
        billing_first_name: this.data["billing_first_name"],
        billing_address_1: this.data["billing_address_1"],
        billing_phone: this.data["billing_phone"],
        user_email: this.data["user_email"],
        shipping_first_name: this.data["shipping_first_name"],
        shipping_address_1: this.data["shipping_address_1"],
    });

please help me out!!

回答1:

You would do this with a normal [(ngModel)] bind.

<ion-label floating>{{'address.email'|translate}}</ion-label>
    <ion-input type="text" [(ngModel)]="user_email"></ion-input>
</ion-item>

Inside your TypeScript you would set the variable to a specific value when the page loads

*.component.ts

constructor() {
    this.user_email = 'some@value.com';
}

This way when the user enters the screen, the value will be the default value and it still allows the user to update that value.



回答2:

You're looking for the "placeholder".

<ion-input placeholder="email@place.com"></ion-input>

https://ionicframework.com/docs/api/components/input/Input/