I have a JSON object using which I create a form having one disabled control and one enabled input control for each value, Disabled inputs are for reference for user while he may change values in active Input control. I need to keep values fixed in disabled Input, while using the same model. Any help will be appreciated.
Asked
Active
Viewed 399 times
1

Gaurav Singh
- 65
- 10
-
It's a template driven form or a reactive form? – Ashish Ranjan Dec 07 '18 at 09:35
-
provide a short example on plunk or stackblitz plz... so that others can help you easily. – Paritosh Dec 07 '18 at 09:38
-
its a template driven form. – Gaurav Singh Dec 07 '18 at 09:38
-
You may find something useful here - https://stackoverflow.com/questions/34375624/angular-2-one-time-binding – Tushar Walzade Dec 07 '18 at 10:38
1 Answers
4
If you are using template driven forms, you can use one-time binding on the disabled elements so they won't update.
This will be even easier with reactive forms as the form model and data model are already separate.
Here is an example (template driven forms):
Normally bound (editable) item:
<input class="form-control"
id="productNameId"
type="text"
placeholder="Name (required)"
required
minlength="3"
[(ngModel)]=product.productName
name="productName" />
One-time binding (non-editable) item:
<input class="form-control"
id="productNameId"
type="text"
placeholder="Name (required)"
required
minlength="3"
ngModel=product.productName /* <- the difference is here */
name="productName" />
Here is a stackblitz: https://stackblitz.com/edit/angular-hnyy5d
[(ngModel)]
-> two-way binding
[ngModel]
-> one-way binding
ngModel
-> one-time binding (only binds the initial value and won't update)

DeborahK
- 57,520
- 12
- 104
- 129
-
1They will update, if you are changing values in the enabled inputs and enabled inputs have two way binding (provided both are using the same variable) – Ashish Ranjan Dec 07 '18 at 09:38
-
1Yes, you are right. I should not try to answer questions at 1:30 AM! I meant "one-time" binding. Fixed in my answer. – DeborahK Dec 07 '18 at 09:41
-
-
-
Did you look at my stackblitz? It demonstrates the technique and is working. – DeborahK Dec 07 '18 at 09:55
-
Thanks a lot its working on my code now . Did not know of such ability of ngModel. Thanks again. – Gaurav Singh Dec 07 '18 at 10:01