Input default value


#1

Hello, I try to get the default value of my Input.

> <ion-item>
>     <ion-input [(ngModel)]="firstname" type="text" value="{{firstname.value}}"></ion-input>
>   </ion-item>

When I try: alert(firstname); it will give me a “undefined”. So how can I get this value?
Thank you.


#2

did you set the default value in your component?

public firstname: string = 'John';

and then you don’t need

value="{{firstname.value}}"

#3

I am getting the default value from an array from the server and output it in the input. So I can not set the default value in the component I think.
The default value is visible in the input and if I change one letter in the input from the default value, then alert(firstname); works… But if I change nothing alert(firstname); will show me undefined.
I am not getting this one, so confusing :sweat_smile:


#4

Then define component variable without any value

public firstname: string;

and when you get the result from the server just set the response value to the this.firstname


#5

That makes no difference :confused:

Let’s suppose I have this code:

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

The default value is ‘John’ and I don’t want to define ‘John’ in the component. I want to get the value from [(ngModel)]="firstname" like I do when I write something in the input.
But it simply seems that it will never output ‘John’ but ‘undefined’ like there was no default value, even though it is output in the input form. :confused:


#6

How exactly are you calling the alert?


#7

Simply with alert(this.firstname); , as I said, it works when I write something in the input, then it shows the value, but it will never show the default value.


#8

What I meant is the context. Could you share some more code?


#9

Correct me if I’m wrong… You are trying to get default value from server and set it to the firstname input…

Remove value=“John”… You don’t need it.

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

In the component define new variable without a value

public firstname: string;

then get data from the server for example in onInit function

ngOnInit() {
    this.userService.getDefaultValues().then(response => {
		this.firstname = response.firstname;
                    alert(this.firstname);
	} );
}

#10

Yes, that is true, but I want to keep the value in the
<ion-input [(ngModel)]="firstname" type="text" value="{{server_response}}"></ion-input> and extract that default value “{{server_response}}” to the component.

I just thought that it should work to call alert(this.firstname); and immediately get the default value. Example:
<ion-input [(ngModel)]="firstname" type="text" value="John"></ion-input> here, calling alert(this.firstname); should show me the value “John” but it actually shows “undefined” as it doesn’t get the value.
It should work the same as if I write something in the input, and then call alert(this.firstname);. Like if I write Hello in the input, it shows me “Hello”…

I think that it’s then not possible to get the default value from
<ion-input [(ngModel)]="firstname" type="text" value="{{firstname.value}}"></ion-input>


#11

What I think that happens is that you are asking for the function and right after that you are triggering the value. But when your software asks for the value of this.firstname, it takes more time than to go to the next line. You have to have a .then () function in order to catch the value and be able to do something with it.


#12

Input is part of a component so you just set default value in that component…
As RaulGM mentioned server doesn’t return response data immediately… it takes a few milliseconds to get the data from a server but alert happens immediately… so you have to wait until server returns that data and then you can set it to the input or alert it…

If you look at my example code above you can see .then() function and inside of this function I use response data and set it to the input through member variable… So this should work… I don’t understand why you need to set firstname directly on the input… When you set it to a member variable and “assign” that variable to the input through [(ngModel)]=“firstname” it should populate the input immediately after server returns expected data…