0

Say I have this setup:

template: <input [(ngModel)]="myValue" type="text" /> <button (click)="addText">Click me</button> {{myValue}}

code:

 addtext()
{
this.myValue='test';
}

If I click the button the inputfield stays empty, but the {{myValue}} is updated. How can I add the text to the inputfield (or better update it, since the nGModel has the real value already)?

Edit:

When clicking the button a child component is shown where the user adds/selects some data. When the child is filled, an event is emitted with the data of the child component. The parent reacts via an "onFilled"-event and changes the model. But since the View doesnt know about the changes, the input is not updated (even the ng-reflect-model is correct).

So my real question is: How to force angular to run an update on the view / rerender it?

Emit-Part:

Child:

@Output myEmitter:new EventEmitter<object>();

filled(){
this.myEmitter.emit({value:'Test'});
}

Parent: (myEmitter)='onMyEmitter($event)' added to input

 onMyEmitter(value) {this.myValue = value}

1 Answer 1

0

I think you are not calling click event function.

<input [(ngModel)]="myValue" type="text" />
<button (click)="addtext()">Click me</button> //addText changed to addtext()
{{myValue}}

You can check it working at : https://angular-h7uata.stackblitz.io

code : https://stackblitz.com/edit/angular-h7uata

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

4 Comments

That was a typo, sorry. But the main problem is the not updating inputfield, not the click event. The original source (where it doesnt work) is a bit more complex, but unfortunately on a "non-internet" machine, so I sketched it here.
Was'nt your goal to display text in the input field? I think the stackblitz example covers that.
I will update my question to try to explain it better.
so do you want to update your value of myValue on changing text in Input field?

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.