检查值是否为 "null"

checking if value is "null"

我正在尝试检查某个值是否为“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{
...
}

MilestoneTypedateComment 值来自 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 结构中尝试了以下内容: this.Milestone.date=="null" this.Milestone.date==="null" Milestone.date=="null" Milestone.date==="null"

当您将 null 设为“null”时,它就是一个字符串。如果你只是想检查一个字段是否为空,那么试试这个

if(!this.Milestone.date){ //empty date field
   this.DateToast();
}
if(!this.Milestone.Commment){//empty comment field
   this.CommentToast();
}