Issue
Do you know how I can make the value 0 disappear in the input? I find it very annoying...
I'm looking to do this using a best practice.
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;
}
Answered By - ferhado
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.