1

Is there an easy way to set the value of an input blank/empty if the value equals zero (0)? I came across this AngularJS question here, Hide Value in input when it's zero in angular Js, but couldn't find anything for Angular 2+.

It would only need to be done on load of the page. I realize I could use a setter in the component, but wondering if there is something that could be put on the input itself.

Could this be done with a directive?

3 Answers 3

1

If you want it done on page load, you could do this:

<input type="number" (ngModelChange)="data = $event === 0 ? '' : $event" [ngModel]="data" >

This will prevent the value from ever being 0, regardless of when.

Sign up to request clarification or add additional context in comments.

5 Comments

The following doesn't work for me: <input type="number" class="form-control" name="constructionYear" [(ngModel)]="vm?.propertyCoveragesTab.dwelling.constructionYear" #constructionYear="ngModel" required pattern="^(18|19|20)\d{2}$" maxlength="4" (ngModelChange)="vm?.propertyCoveragesTab.dwelling.constructionYear = $event === 0 ? '' : $event" />
You cannot have parenthesis around ngModel. Make sure you follow the example
I removed my parenthesis around ngModel but the zero value still gets set: <input type="number" class="form-control" name="constructionYear" [ngModel]="vm?.propertyCoveragesTab.dwelling.constructionYear" #constructionYear="ngModel" required pattern="^(18|19|20)\d{2}$" maxlength="4" (ngModelChange)="vm?.propertyCoveragesTab.dwelling.constructionYear = $event === 0 ? '' : $event" />
You're not following the example. see here stackblitz.com/edit/…
Got it. Your example works for me after page load if I manually type in 0. However, it doesn't appear to work on page load: stackblitz.com/edit/angular-z3fhq2?file=app/app.component.ts
1

You could use a template variable.

checkInputValue(input) {
  input.value = input.value == '0' ? '' : input.value;
}

and then

<input #input (keyup)="checkInputValue(input)" />

Edit for onload :

import { ViewChild, ElementRef, AfterViewInit } from '@angular/core'

// ...

export class Component implements AfterViewInit {

  @ViewChild('input') input: ElementRef

  ngAfterViewInit() {
    let inputValue = this.input.nativeElement.value;
    inputValue = inputValue == '0' ? '' : inputValue;
  }
}

3 Comments

I'm looking for something that can be done onload of the page. I edited my question.
Thanks. Your example does work. Do you know if a directive or property could be put directly on the input in the component.html rather than having logic inside the component.ts?
Hmm, I haven't tested this but you could try with the value attribute directive <input #input [value]="input.value == '0' ? '' : input.value " />
0

To make it work on load, Modifying the ngModel would be suffice:

<input type="number" [ngModel]="data == '0' ? '' : data" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.