ngModel on the same form field as formControlName. Support for using the ngModel input property and...












0














So does it mean that we are not going enjoy the glamour of two way binding?



For ex: In NG6 I have a customer list(custList) which creates a reactive form and the ngModel is being used for 2-way data binding. When change a property from UI for a specific customer, it reflects in custList too. Angular code for NG6 as follows;



In My Component;



let custForm: FormGroup;
let custFormArray = new Array();

custList.forEach(cust => {
custFormArray.push(new FormGroup({
id: new FormControl(cust .id),
name: new FormControl(cust.name, [Validators.required])
}));
});

this.custForm = this.fb.group({
custFormArray: this.fb.array(custFormArray)
});


In my template;



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id" [(ngModel)]="cust.id">
<input formControlName="name" [(ngModel)]="cust.name">
</div>
</div>
</div>
</div>


New Way:(No ngModel binding)



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id">
<input formControlName="name">
</div>
</div>
</div>
</div>


When I use the above way, a change from the UI doesn't propagate the custList which breaks the two way data binding model. Only way I can access the changes via the following;



let customerList =Object.assign({}, this.custForm.value.custFormArray) as   Array;

let specificCustomer = customerList[specificCustomerIndex];


where custForm is the formName and custFormArray is the form array.



Means that I need to do the same thing wherever I want to access the modified list.



Can anybody answer me whether this is intentional in the new version?










share|improve this question






















  • The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
    – Octavio Garbarino
    Nov 21 at 0:52










  • Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
    – user3866099
    Nov 28 at 3:52










  • If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
    – Octavio Garbarino
    Nov 28 at 10:50
















0














So does it mean that we are not going enjoy the glamour of two way binding?



For ex: In NG6 I have a customer list(custList) which creates a reactive form and the ngModel is being used for 2-way data binding. When change a property from UI for a specific customer, it reflects in custList too. Angular code for NG6 as follows;



In My Component;



let custForm: FormGroup;
let custFormArray = new Array();

custList.forEach(cust => {
custFormArray.push(new FormGroup({
id: new FormControl(cust .id),
name: new FormControl(cust.name, [Validators.required])
}));
});

this.custForm = this.fb.group({
custFormArray: this.fb.array(custFormArray)
});


In my template;



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id" [(ngModel)]="cust.id">
<input formControlName="name" [(ngModel)]="cust.name">
</div>
</div>
</div>
</div>


New Way:(No ngModel binding)



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id">
<input formControlName="name">
</div>
</div>
</div>
</div>


When I use the above way, a change from the UI doesn't propagate the custList which breaks the two way data binding model. Only way I can access the changes via the following;



let customerList =Object.assign({}, this.custForm.value.custFormArray) as   Array;

let specificCustomer = customerList[specificCustomerIndex];


where custForm is the formName and custFormArray is the form array.



Means that I need to do the same thing wherever I want to access the modified list.



Can anybody answer me whether this is intentional in the new version?










share|improve this question






















  • The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
    – Octavio Garbarino
    Nov 21 at 0:52










  • Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
    – user3866099
    Nov 28 at 3:52










  • If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
    – Octavio Garbarino
    Nov 28 at 10:50














0












0








0







So does it mean that we are not going enjoy the glamour of two way binding?



For ex: In NG6 I have a customer list(custList) which creates a reactive form and the ngModel is being used for 2-way data binding. When change a property from UI for a specific customer, it reflects in custList too. Angular code for NG6 as follows;



In My Component;



let custForm: FormGroup;
let custFormArray = new Array();

custList.forEach(cust => {
custFormArray.push(new FormGroup({
id: new FormControl(cust .id),
name: new FormControl(cust.name, [Validators.required])
}));
});

this.custForm = this.fb.group({
custFormArray: this.fb.array(custFormArray)
});


In my template;



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id" [(ngModel)]="cust.id">
<input formControlName="name" [(ngModel)]="cust.name">
</div>
</div>
</div>
</div>


New Way:(No ngModel binding)



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id">
<input formControlName="name">
</div>
</div>
</div>
</div>


When I use the above way, a change from the UI doesn't propagate the custList which breaks the two way data binding model. Only way I can access the changes via the following;



let customerList =Object.assign({}, this.custForm.value.custFormArray) as   Array;

let specificCustomer = customerList[specificCustomerIndex];


where custForm is the formName and custFormArray is the form array.



Means that I need to do the same thing wherever I want to access the modified list.



Can anybody answer me whether this is intentional in the new version?










share|improve this question













So does it mean that we are not going enjoy the glamour of two way binding?



For ex: In NG6 I have a customer list(custList) which creates a reactive form and the ngModel is being used for 2-way data binding. When change a property from UI for a specific customer, it reflects in custList too. Angular code for NG6 as follows;



In My Component;



let custForm: FormGroup;
let custFormArray = new Array();

custList.forEach(cust => {
custFormArray.push(new FormGroup({
id: new FormControl(cust .id),
name: new FormControl(cust.name, [Validators.required])
}));
});

this.custForm = this.fb.group({
custFormArray: this.fb.array(custFormArray)
});


In my template;



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id" [(ngModel)]="cust.id">
<input formControlName="name" [(ngModel)]="cust.name">
</div>
</div>
</div>
</div>


New Way:(No ngModel binding)



<div *ngIf="custList?.length > 0" [formGroup]="custForm">
<div formArrayName="custFormArray">
<div *ngFor="let cust of custList; let i=index">
<div [formGroupName]="i">
<input formControlName="id">
<input formControlName="name">
</div>
</div>
</div>
</div>


When I use the above way, a change from the UI doesn't propagate the custList which breaks the two way data binding model. Only way I can access the changes via the following;



let customerList =Object.assign({}, this.custForm.value.custFormArray) as   Array;

let specificCustomer = customerList[specificCustomerIndex];


where custForm is the formName and custFormArray is the form array.



Means that I need to do the same thing wherever I want to access the modified list.



Can anybody answer me whether this is intentional in the new version?







angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 0:29









user3866099

1




1












  • The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
    – Octavio Garbarino
    Nov 21 at 0:52










  • Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
    – user3866099
    Nov 28 at 3:52










  • If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
    – Octavio Garbarino
    Nov 28 at 10:50


















  • The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
    – Octavio Garbarino
    Nov 21 at 0:52










  • Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
    – user3866099
    Nov 28 at 3:52










  • If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
    – Octavio Garbarino
    Nov 28 at 10:50
















The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
– Octavio Garbarino
Nov 21 at 0:52




The 2 way databinding is still working but you have to chose between reactve forms (formGroups, formArrays and formControls) and template-driven forms ( [(ngModel)] ). If I am looking your code fine, the [formGroupName]="i" part of the code is what makes the mess there, try removing it and the 2 way databinding should works if I am getting your code ok
– Octavio Garbarino
Nov 21 at 0:52












Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
– user3866099
Nov 28 at 3:52




Well, I have multiple customers in the custList and need to display a block for each customer. The unique reference to each customer is the "i". Measn that I cant remove the indexer.
– user3866099
Nov 28 at 3:52












If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
– Octavio Garbarino
Nov 28 at 10:50




If I am not wrong you should choose between using [(ngModel)] or formControlName, it is not recomended to use both kind of forms in the same form (reactive form angular.io/guide/reactive-forms) (and template form angular.io/guide/forms#introduction-to-template-driven-forms)
– Octavio Garbarino
Nov 28 at 10:50

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403649%2fngmodel-on-the-same-form-field-as-formcontrolname-support-for-using-the-ngmodel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403649%2fngmodel-on-the-same-form-field-as-formcontrolname-support-for-using-the-ngmodel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)