Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove the value 0 from the input

Do you know how I can make the value 0 disappear in the input? I find it very annoying…

enter image description here

I’m looking to do this using a best practice.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export class AppComponent implements OnInit { 

  nb: number = 0; 
  resultElement: string = "";

  ngOnInit(): void {

  }

  checkNumber(): void {
    if(this.nb > 0){
      this.resultElement = "The number is positive";
    } else if (this.nb < 0){
      this.resultElement = "The number is negative";
    } else {
      this.resultElement = "The number is zero";
    }
  }

}

If I replace it

nb: number = 0;

With that

nb: any; 

Or

nb: number | null = null;

The problem is solved, but is it the best practice?

>Solution :

Use the ! postfix expression with the property declaration. It tells TypeScript that the property will definitely be assigned. For example:

export class AppComponent implements OnInit { 
  nb!: number;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading