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


#1

Hi,
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:
.html:

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

.ts:

goal:any;
button1 :any;

getValues(){
		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.

Greetings,
Robert


#2

@RobertLinde You should change your method to handle that:

getValues(){
	this.global.goal = (this.goal != null) ? this.goal : this.global.goal;
	this.global.button1 = (this.button1 != null) ? this.button1 : this.global.button1;
}

#3

Great, this works! Thanks a lot!