[Solved] Don´t change variable if input=null

In my app, there is the option to enter some strings as input, but you don´t have to enter a value. If you do not enter a value, it should just keep the default-value of the variable and not change it to null. I thought about an if-clause but it didn´t work but maybe somebody here got an idea.

My code:

<ion-content padding>
			<ion-input placeholder="{{global.goal}}" [(ngModel)]="goal"></ion-input>
			<ion-input placeholder="{{global.button1}}" [(ngModel)]="button1"></ion-input>
<button  ion-button (click)="getValues()">save</button>


button1 :any;

		this.global.goal = this.goal;
		this.global.button1 = this.button1;

So basically, the global variables should keep the value they had before if the input is empty and not change to null.

If somebody has an idea how to solve it, especially code, I would be very happy about it.


@RobertLinde You should change your method to handle that:

	this.global.goal = (this.goal != null) ? this.goal : this.global.goal;
	this.global.button1 = (this.button1 != null) ? this.button1 : this.global.button1;
1 Like

Great, this works! Thanks a lot!