11

I tried to put the value to <input value="{{ val }}"> but I think its wrong because it returns empty data.

How can I set a default value to an input?

I already created API for update. I just really need to return the input data.

thanks

1
  • "I already created API for update" - so what do you do? Do you look at the dom and values for the controls? I think it would be better to use ngModel and do a proper binding. You can create a separate object for your form and set the initial data whenever you want (just at the start?) like this.formData = Object.assign({}, this.data);, then use formData when you submit the form leaving your original data unchanged. Commented Sep 8, 2017 at 0:38

2 Answers 2

17

You can use one-way input binding:

<input [value]="val">

or two-way input binding:

<input [(ngModel)]="val">

One-way will give you the value, and update it when the component updates it... but will not change the value with user input.

Two-way will change on user input as well as component changes.

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

Comments

3

use this. It will show the data as well as do a two way binding. you can change its value from code to view and from view to code.

<input [(ngModel)]="val">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.