【问题标题】:checking if value is "null"检查值是否为“null”
【发布时间】:2021-01-08 20:59:11
【问题描述】:

我正在尝试检查一个值是否为“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"

【问题讨论】:

标签: html angular typescript ionic-framework ionic4


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2014-07-16
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多