0

I have a property in an object in an Angular service that is bound to a text field in a component's html.

When the property is updated by the service, the new value is not showing in the html element until I click on the element.

I cannot figure out why the new value isn't immediately showing in the text field without having to click on the element to get the new value.

The console.log message in the update function shows that the object is updated correctly, it's just that the data is not making it through to the form field until the element is clicked.

The code is below:

parent.component.html

<input [(ngModel)]="service.serviceObj.data" type="text" />

service.service.ts

public serviceObj = {} as any;

...

updateObj() {
    this.serviceObj.data = 'new value';
    console.log('this.serviceObj = ', this.serviceObj);
}

...
1
  • How do you call updateObj() in the parent.component.ts? We need more code to find out your problem… Commented Jan 18, 2022 at 22:23

2 Answers 2

1

Your input is referencing:

service.selectedMarker.data

But your setting:

this.serviceObj.data = 'new value'

serviceObj vs selectedMarker. Did you mean to do this?

<input [(ngModel)]="service.serviceObj.data" type="text" />

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

1 Comment

Sorry, that was a typo. I've just updated the OP with serviceObj.
0

you can always make a copy of the object like this:

 const obj= JSON.parse(JSON.stringify(<objToCopy>));

then make the mutation in the obj and assign the service property to the mutated copy obj ("this.yourObj = obj")

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.