【问题标题】:Input field with spaces only not allow to save in angular?仅带空格的输入字段不允许以角度保存?
【发布时间】:2018-10-19 06:12:38
【问题描述】:

这里我有一个输入字段,当我单击SAVE 按钮时,它会进入saveNewCategory() 函数并检查它是否为空,但是当我在输入字段中只输入空格时,它也会被保存,所以它是如何保存的输入字段只给出空格时不允许保存输入字段?

category.component.html

<div>
  <form method="post" enctype="multipart/form-data">
    <mat-form-field>
     <input matInput placeholder="Category Title" maxlength="30" name="categorytitle" [(ngModel)]="this.categoryObj.categorytitle" required>
    </mat-form-field>
  </form>
  <button mat-raised-button color= "accent" (click)="saveNewCategory()">SAVE</button>
</div>

category.component.ts

saveNewCategory(){
  if(this.categoryObj.categorytitle != ''){
    const formData = new FormData();
    formData.append('categorytitle',this.categoryObj.categorytitle);
    this.categoryService.saveNewCategory(formData).subscribe(
      (data) => {
        if(data != undefined && data.status == 1){
          this.snackBar.open('New Category Saved Successfully..!!', '',{
            duration: 2000
          });  
        }
      }
    )
  }else{
    this.snackBar.open('Category Image is required..!!', '',{
      duration: 2000
    });  
  }  
} 

【问题讨论】:

标签: html angular


【解决方案1】:

您可以将trim 方法用作:

saveNewCategory(){
  if(this.categoryObj.categorytitle.trim() != ''){ <===== Here
    const formData = new FormData();
    formData.append('categorytitle',this.categoryObj.categorytitle);
    this.categoryService.saveNewCategory(formData).subscribe(
      ...
    )
  }else{
    ...
  }  
} 

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多