我正在尝试检查一个值是否为"null“,但我不知道如何检查。下面是我的代码:
Milestone: any={};
constructor(public toastController: ToastController) {
this.Milestone.MilestoneType='';
this.Milestone.date='';
this.Milestone.Comment='';
}
onSubmit(){
var Milestone = JSON.stringify({
MilestoneType: this.Milestone.MilestoneType,
date: this.Milestone.date,
Comment: this.Milestone.Comment,
ID: this.current.id});
if( this.Milestone.date=="null"){
this.DateToast();
}
else if( this.Milestone.Commment==='null'){
this.CommentToast();
}
else{
...
}MilestoneType、date和Comment的值来自HTML文件:
<ion-segment name="MilestoneType" [(ngModel)]="Milestone.MilestoneType" [ngModelOptions]="{standalone: true}" >
<ion-segment-button value="Plant" >Plant</ion-segment-button>
<ion-segment-button value="Fertilizer" >Fertilizer</ion-segment-button>
<ion-segment-button value="Other" >Other</ion-segment-button>
</ion-segment>
</ion-toolbar>
<div [ngSwitch]="true" >
<ion-card *ngSwitchCase="Milestone.MilestoneType=== 'Plant' || Milestone.MilestoneType==='Fertilizer' || Milestone.MilestoneType==='Other'">
<p>Type: {{Milestone.MilestoneType}}</p>
<label for="date">Date: </label>
<input required name="date" [(ngModel)]="Milestone.date" type="date" clearInput="true">
<br>
<div class="form-group">
<label for="Comment" >Comment:</label>
<input id="comment" name="Comment" [(ngModel)]="Milestone.Comment" clearInput="true">
</div>我的目标是检查一个值是否为null .If,因此,我需要显示一条消息。我在If结构中尝试了以下内容:this.Milestone.date=="null" this.Milestone.date==="null" Milestone.date=="null" Milestone.date==="null"
发布于 2020-09-23 01:52:31
当你把null和"null“放在一起时,它就是一个字符串。如果您只是想检查字段是否为空,请尝试以下操作
if(!this.Milestone.date){ //empty date field
this.DateToast();
}
if(!this.Milestone.Commment){//empty comment field
this.CommentToast();
} https://stackoverflow.com/questions/64014734
复制相似问题