0

I'm using Angular 8.1.3. Here's my HTML...

<mat-form-field floatLabel="never">
  <input matInput placeholder="Address Line 2" [value]="addressLine2" [disabled]="disableAddressFields">
</mat-form-field>

I have the following in my TypeScript file.

addressLine2 = '';

Changing to a non-empty string is reflected in the HTML.

this.addressLine2 = 'some value';

But setting it back to an empty string does is not reflected in the HTML, despite the value being empty ('') in the TypeScript (confirmed by printing to console).

this.addressLine2 = '';

It seems that once a non-empty string is set in the TypeScript, an empty string that is set is not reflected in the HTML. I'm kind of lost here; any help would be greatly appreciated.

0

2 Answers 2

2

For two way binding to work you need to use [(ngModel)]. Import angular forms add add [(ngModel)] to your code.

 <input matInput placeholder="Address Line 2" [(ngModel)]="addressLine2" [disabled]="disableAddressFields">

In your current code you are binding input's value property to addressLine2 so it is working as expected. To also get data back from input field you need to use ngModel

  <input matInput placeholder="Address Line 2" [value]="addressLine2" [disabled]="disableAddressFields">
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, that fixes it! Thank you for the detailed answer. :)
@alt255 - Why do you say "In your current code you are binding input's value property to disableAddressFields"?
@ConnorsFan it was a typo 😅. fixed it
0

Instead of [value]="addressline2", it should be [(ngmodel)]="addressline2" for two way binding

1 Comment

Not if the OP is using forms. Using [(ngModel)] within ngForm was removed in v7.

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.