0

I'm using reactive forms in an Angular application. I have 2 fields, one is a select (named client) and the other one an input (named clientCode) which has to be updated the user selects a new client.

<select formControlName="client">
            <option value="">Client</option>
            <option *ngFor="let item of data.clients" [value]="item.value">{{ item.value }}</option>
          </select>
          <input type="text" formControlName="clientCode" placeholder="Client Code" value="{{ clientCode$ | async }}">

I'm using valueChanges over client, so that when the user selects a client I get the code associated to that client and it returns it as an Observable (clientCode$) which then updates the input value.

this.clientCode$ = this.summary
      .get('client')
      .valueChanges.map(val => this.clientsMap.get(val));

This is working in my screen, as the clientCode is displayed, but the form itself is not updated. Only when I click in the input field and type something is updated.

Is there any way to fix this?

Thanks

1
  • Moving {{ clientCode$ }} from value= to inside inside input tags might help: <input ... >{{ clientCode$ }}</input>. Also, closing your input field may fix it: <input ... / > or <input ...></input> Commented Jun 17, 2017 at 13:14

1 Answer 1

3

In order to update control model successfully you can use ngModel property instead of value.

It might look like:

[ngModel]="clientCode$ | async"

Plunker Example

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

1 Comment

@r0b3rt0 That's good that you read the documentation but please don't write such comments if you don't know how it works. In my plunker example i even didn't import FormsModule. ngModel is just @Input for reactive FormControlName

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.