Adding dynamically added field values along with form values in angular












0















I have an component called demo, which as some i/p fields:
first name,last name,number.... along with these fields i am dynamically adding the 2 fields (i,e to add multiple address).




I am able to send the all form fields value but not the dynamically added values.




While logging the dynamically added field values are displaying as undefined. Since the component's code is long, I am posting stackblitz DEMO.










share|improve this question























  • which 2 fields are you talking about? addresses are the array.

    – Just code
    Dec 31 '18 at 4:19











  • Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

    – Empty_Soul
    Dec 31 '18 at 4:25


















0















I have an component called demo, which as some i/p fields:
first name,last name,number.... along with these fields i am dynamically adding the 2 fields (i,e to add multiple address).




I am able to send the all form fields value but not the dynamically added values.




While logging the dynamically added field values are displaying as undefined. Since the component's code is long, I am posting stackblitz DEMO.










share|improve this question























  • which 2 fields are you talking about? addresses are the array.

    – Just code
    Dec 31 '18 at 4:19











  • Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

    – Empty_Soul
    Dec 31 '18 at 4:25
















0












0








0








I have an component called demo, which as some i/p fields:
first name,last name,number.... along with these fields i am dynamically adding the 2 fields (i,e to add multiple address).




I am able to send the all form fields value but not the dynamically added values.




While logging the dynamically added field values are displaying as undefined. Since the component's code is long, I am posting stackblitz DEMO.










share|improve this question














I have an component called demo, which as some i/p fields:
first name,last name,number.... along with these fields i am dynamically adding the 2 fields (i,e to add multiple address).




I am able to send the all form fields value but not the dynamically added values.




While logging the dynamically added field values are displaying as undefined. Since the component's code is long, I am posting stackblitz DEMO.







angular typescript angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 4:09









Empty_SoulEmpty_Soul

31611




31611













  • which 2 fields are you talking about? addresses are the array.

    – Just code
    Dec 31 '18 at 4:19











  • Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

    – Empty_Soul
    Dec 31 '18 at 4:25





















  • which 2 fields are you talking about? addresses are the array.

    – Just code
    Dec 31 '18 at 4:19











  • Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

    – Empty_Soul
    Dec 31 '18 at 4:25



















which 2 fields are you talking about? addresses are the array.

– Just code
Dec 31 '18 at 4:19





which 2 fields are you talking about? addresses are the array.

– Just code
Dec 31 '18 at 4:19













Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

– Empty_Soul
Dec 31 '18 at 4:25







Just fill all input fields and also the dynamic fields and hit the Save button then see the console, Then you can understand my issue.

– Empty_Soul
Dec 31 '18 at 4:25














1 Answer
1






active

oldest

votes


















3














As addresstype is array and your will not be able to get it easily,



  addFieldValue() {
if (this.newAttribute.adr && this.newAttribute.city) {
if (this.fieldArray.indexOf(this.newAttribute) === -1) {
this.fieldArray.push(this.newAttribute)
}
}

this.newAttribute = {};//see here
}


Here you are actually emptying the value so the values will be always 0.



But you are already putting the values in the fieldArray, you can easily retrieve the array values by iterating over it and getting the values.



  public onAdd(): void {
this.someContact = this.addForm.value;
this.someContact.phoneNumbers = ;
const phno: IPhoneNumber = {
countryCode: this.addForm.value.counrtycode,
number: this.addForm.value.phonenumber,
};
this.someContact.phoneNumbers.push(phno);

this.someContact.addresses = ;
this.fieldArray.forEach(a => {
const addr: IPostalAddress = {
addtype: a.adr,
city: a.city,
};
this.someContact.addresses.push(addr);
})


console.log(this.someContact);
}


I changed type to addtype, looks bad naming.



export interface IPostalAddress {
addtype: string;
city: string;
}


Demo



Note: as you are not dealing with the forms you can remove formControlName="adrType"






share|improve this answer



















  • 1





    Thanks for answer and also for your valuable explanation regarding my issue.

    – Empty_Soul
    Dec 31 '18 at 4:33











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%2f53983501%2fadding-dynamically-added-field-values-along-with-form-values-in-angular%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














As addresstype is array and your will not be able to get it easily,



  addFieldValue() {
if (this.newAttribute.adr && this.newAttribute.city) {
if (this.fieldArray.indexOf(this.newAttribute) === -1) {
this.fieldArray.push(this.newAttribute)
}
}

this.newAttribute = {};//see here
}


Here you are actually emptying the value so the values will be always 0.



But you are already putting the values in the fieldArray, you can easily retrieve the array values by iterating over it and getting the values.



  public onAdd(): void {
this.someContact = this.addForm.value;
this.someContact.phoneNumbers = ;
const phno: IPhoneNumber = {
countryCode: this.addForm.value.counrtycode,
number: this.addForm.value.phonenumber,
};
this.someContact.phoneNumbers.push(phno);

this.someContact.addresses = ;
this.fieldArray.forEach(a => {
const addr: IPostalAddress = {
addtype: a.adr,
city: a.city,
};
this.someContact.addresses.push(addr);
})


console.log(this.someContact);
}


I changed type to addtype, looks bad naming.



export interface IPostalAddress {
addtype: string;
city: string;
}


Demo



Note: as you are not dealing with the forms you can remove formControlName="adrType"






share|improve this answer



















  • 1





    Thanks for answer and also for your valuable explanation regarding my issue.

    – Empty_Soul
    Dec 31 '18 at 4:33
















3














As addresstype is array and your will not be able to get it easily,



  addFieldValue() {
if (this.newAttribute.adr && this.newAttribute.city) {
if (this.fieldArray.indexOf(this.newAttribute) === -1) {
this.fieldArray.push(this.newAttribute)
}
}

this.newAttribute = {};//see here
}


Here you are actually emptying the value so the values will be always 0.



But you are already putting the values in the fieldArray, you can easily retrieve the array values by iterating over it and getting the values.



  public onAdd(): void {
this.someContact = this.addForm.value;
this.someContact.phoneNumbers = ;
const phno: IPhoneNumber = {
countryCode: this.addForm.value.counrtycode,
number: this.addForm.value.phonenumber,
};
this.someContact.phoneNumbers.push(phno);

this.someContact.addresses = ;
this.fieldArray.forEach(a => {
const addr: IPostalAddress = {
addtype: a.adr,
city: a.city,
};
this.someContact.addresses.push(addr);
})


console.log(this.someContact);
}


I changed type to addtype, looks bad naming.



export interface IPostalAddress {
addtype: string;
city: string;
}


Demo



Note: as you are not dealing with the forms you can remove formControlName="adrType"






share|improve this answer



















  • 1





    Thanks for answer and also for your valuable explanation regarding my issue.

    – Empty_Soul
    Dec 31 '18 at 4:33














3












3








3







As addresstype is array and your will not be able to get it easily,



  addFieldValue() {
if (this.newAttribute.adr && this.newAttribute.city) {
if (this.fieldArray.indexOf(this.newAttribute) === -1) {
this.fieldArray.push(this.newAttribute)
}
}

this.newAttribute = {};//see here
}


Here you are actually emptying the value so the values will be always 0.



But you are already putting the values in the fieldArray, you can easily retrieve the array values by iterating over it and getting the values.



  public onAdd(): void {
this.someContact = this.addForm.value;
this.someContact.phoneNumbers = ;
const phno: IPhoneNumber = {
countryCode: this.addForm.value.counrtycode,
number: this.addForm.value.phonenumber,
};
this.someContact.phoneNumbers.push(phno);

this.someContact.addresses = ;
this.fieldArray.forEach(a => {
const addr: IPostalAddress = {
addtype: a.adr,
city: a.city,
};
this.someContact.addresses.push(addr);
})


console.log(this.someContact);
}


I changed type to addtype, looks bad naming.



export interface IPostalAddress {
addtype: string;
city: string;
}


Demo



Note: as you are not dealing with the forms you can remove formControlName="adrType"






share|improve this answer













As addresstype is array and your will not be able to get it easily,



  addFieldValue() {
if (this.newAttribute.adr && this.newAttribute.city) {
if (this.fieldArray.indexOf(this.newAttribute) === -1) {
this.fieldArray.push(this.newAttribute)
}
}

this.newAttribute = {};//see here
}


Here you are actually emptying the value so the values will be always 0.



But you are already putting the values in the fieldArray, you can easily retrieve the array values by iterating over it and getting the values.



  public onAdd(): void {
this.someContact = this.addForm.value;
this.someContact.phoneNumbers = ;
const phno: IPhoneNumber = {
countryCode: this.addForm.value.counrtycode,
number: this.addForm.value.phonenumber,
};
this.someContact.phoneNumbers.push(phno);

this.someContact.addresses = ;
this.fieldArray.forEach(a => {
const addr: IPostalAddress = {
addtype: a.adr,
city: a.city,
};
this.someContact.addresses.push(addr);
})


console.log(this.someContact);
}


I changed type to addtype, looks bad naming.



export interface IPostalAddress {
addtype: string;
city: string;
}


Demo



Note: as you are not dealing with the forms you can remove formControlName="adrType"







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 4:29









Just codeJust code

10.4k53066




10.4k53066








  • 1





    Thanks for answer and also for your valuable explanation regarding my issue.

    – Empty_Soul
    Dec 31 '18 at 4:33














  • 1





    Thanks for answer and also for your valuable explanation regarding my issue.

    – Empty_Soul
    Dec 31 '18 at 4:33








1




1





Thanks for answer and also for your valuable explanation regarding my issue.

– Empty_Soul
Dec 31 '18 at 4:33





Thanks for answer and also for your valuable explanation regarding my issue.

– Empty_Soul
Dec 31 '18 at 4:33


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53983501%2fadding-dynamically-added-field-values-along-with-form-values-in-angular%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas