How to remove the child when removing the parent?












0















I am making angular application with angular dynamic form where i am using ng-select library.



The HTML with select:



    <div *ngFor="let question of questions" class="form-row {{question.class}}">
<ng-container *ngIf="question.children">
<div [formArrayName]="question.key" class="w-100">
<div *ngFor="let item of form.get(question.key).controls; let i=index" [formGroupName]="i" class="row mt-1">
<div *ngFor="let item of question.children" class="{{item.class}} align-middle">
<div class="w-100">
<dynamic-form-builder [question]="item" [index]="i" [form]="form.get(question.key).at(i)"></dynamic-form-builder>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-6 col-sm-12 col-lg-6 col-md-6">
<div class="form-label-group" *ngIf="showTemplateDropdown">
<ngi-select placeholder="Select Template" [required]="true" [hideSelected]="false" [multiple]="true" [items]="templateList"
dropdownPosition="down" bindLabel="name" bindValue="id" (add)="getTemplateValues($event)" (remove)="onRemove($event)">
</ngi-select>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-6 col-sm-12 col-lg-6 col-md-6">

</div>
<div class="col-6 col-sm-12 col-lg-6 col-md-6 text-right">
<div class="btn-group float-right">
<button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
(click)="addControls('template_properties')">
<span class="mat-button-wrapper"><i class="material-icons mt-2">add</i></span>
<div class="mat-button-ripple mat-ripple" matripple=""></div>
<div class="mat-button-focus-overlay"></div>
</button> &nbsp;&nbsp;&nbsp;
<button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
(click)="removeControls('template_properties')">
<span class="mat-button-wrapper"><i class="material-icons mt-2">remove</i></span>
<div class="mat-button-ripple mat-ripple" matripple=""></div>
<div class="mat-button-focus-overlay"></div>
</button>
</div>
</div>
</div>
</div>

</div>
</ng-container>
<ng-container *ngIf="!question.children">
<div class="w-100">
<dynamic-form-builder [question]="question" [form]="form"></dynamic-form-builder>
</div>
</ng-container>
</div>


Here the [items]="templateList" has the following,



[{"id":"5bebba2c20ccc52871509d56","name":"Template One"},
{"id":"5bebba5720ccc52871509d57","name":"Template Two"},
{"id":"5bebba8d20ccc52871509d5d","name":"Template Three"}]


I am having (change)="getTemplateValues($event)" event for detecting each change happen when we select an item from dropdown.



The change event function Edited,



    getTemplateValues(e) {
this.dynamicFormService.getRest("url" + '/' + e.id").subscribe(res => {
try {
if (res.status === "true") {
res.data.template_properties.forEach(element => {
this.templateArray.push(element);
});
this.form = this.qcs.toFormGroup(this.questions);
for (let i = 0; i < this.templateArray.length; i++) {
this.addControls('template_properties');
}
let propertiesArray = ;
this.templateArray.forEach(element => {
propertiesArray.push(element);
});
this.form.patchValue({
'template_properties': propertiesArray
});
} else {

}
}
catch (error) {

}
})


}



console.log(this.propertiesArray) gives the following,



[{"property_name":"Property one","property_type":4,"property_required":true,"property_origin":1},{"property_name":"Property one","property_type":5,"property_required":true,"property_origin":1}]  


In the below image i have deleted template three but the template three properties still showing in it..



enter image description here



Here first i am filtering the data first and ignoring the duplicates and each time i am sending the newly selected values alone to the service and fetching the data related to the id element.id.



And using this.addControls('template_properties') to make open the number of rows, and elements will get patched to the form template_properties.



this.form.patchValue({
'template_properties': propertiesArray
});


As of now everything working fine..



The problem actually arise from here:



If we delete a selected list from dropdown, (say i have selected all three template and i have deleted the template two then that particular template's template_properties needs to get deleted..



I have tried with (remove)="onRemove($event)" but its not working because while remove data, the (change) function also calls..



How can i remove the template_properties with this.removeControls('template_properties'); of particular deleted template name in the change event or remove event..



Remove Function:



  onRemove(e) {
console.log(e);
this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
try {
if (res.status === "true") {
for (let i = 0; i < res.data.template_properties.length; i++) {
this.removeControls('template_properties');
}
} else {

}
}
catch (error) {

}
})
}


Remove Control:



  removeControls(control: string) {
let array = this.form.get(control) as FormArray;
console.log(array)
array.removeAt(array.length);
}


console.log(array) gives,



enter image description here










share|improve this question





























    0















    I am making angular application with angular dynamic form where i am using ng-select library.



    The HTML with select:



        <div *ngFor="let question of questions" class="form-row {{question.class}}">
    <ng-container *ngIf="question.children">
    <div [formArrayName]="question.key" class="w-100">
    <div *ngFor="let item of form.get(question.key).controls; let i=index" [formGroupName]="i" class="row mt-1">
    <div *ngFor="let item of question.children" class="{{item.class}} align-middle">
    <div class="w-100">
    <dynamic-form-builder [question]="item" [index]="i" [form]="form.get(question.key).at(i)"></dynamic-form-builder>
    </div>
    </div>
    </div>
    <div class="container">
    <div class="row">
    <div class="col-6 col-sm-12 col-lg-6 col-md-6">
    <div class="form-label-group" *ngIf="showTemplateDropdown">
    <ngi-select placeholder="Select Template" [required]="true" [hideSelected]="false" [multiple]="true" [items]="templateList"
    dropdownPosition="down" bindLabel="name" bindValue="id" (add)="getTemplateValues($event)" (remove)="onRemove($event)">
    </ngi-select>
    </div>
    </div>
    </div>
    </div>
    <div class="container">
    <div class="row">
    <div class="col-6 col-sm-12 col-lg-6 col-md-6">

    </div>
    <div class="col-6 col-sm-12 col-lg-6 col-md-6 text-right">
    <div class="btn-group float-right">
    <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
    (click)="addControls('template_properties')">
    <span class="mat-button-wrapper"><i class="material-icons mt-2">add</i></span>
    <div class="mat-button-ripple mat-ripple" matripple=""></div>
    <div class="mat-button-focus-overlay"></div>
    </button> &nbsp;&nbsp;&nbsp;
    <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
    (click)="removeControls('template_properties')">
    <span class="mat-button-wrapper"><i class="material-icons mt-2">remove</i></span>
    <div class="mat-button-ripple mat-ripple" matripple=""></div>
    <div class="mat-button-focus-overlay"></div>
    </button>
    </div>
    </div>
    </div>
    </div>

    </div>
    </ng-container>
    <ng-container *ngIf="!question.children">
    <div class="w-100">
    <dynamic-form-builder [question]="question" [form]="form"></dynamic-form-builder>
    </div>
    </ng-container>
    </div>


    Here the [items]="templateList" has the following,



    [{"id":"5bebba2c20ccc52871509d56","name":"Template One"},
    {"id":"5bebba5720ccc52871509d57","name":"Template Two"},
    {"id":"5bebba8d20ccc52871509d5d","name":"Template Three"}]


    I am having (change)="getTemplateValues($event)" event for detecting each change happen when we select an item from dropdown.



    The change event function Edited,



        getTemplateValues(e) {
    this.dynamicFormService.getRest("url" + '/' + e.id").subscribe(res => {
    try {
    if (res.status === "true") {
    res.data.template_properties.forEach(element => {
    this.templateArray.push(element);
    });
    this.form = this.qcs.toFormGroup(this.questions);
    for (let i = 0; i < this.templateArray.length; i++) {
    this.addControls('template_properties');
    }
    let propertiesArray = ;
    this.templateArray.forEach(element => {
    propertiesArray.push(element);
    });
    this.form.patchValue({
    'template_properties': propertiesArray
    });
    } else {

    }
    }
    catch (error) {

    }
    })


    }



    console.log(this.propertiesArray) gives the following,



    [{"property_name":"Property one","property_type":4,"property_required":true,"property_origin":1},{"property_name":"Property one","property_type":5,"property_required":true,"property_origin":1}]  


    In the below image i have deleted template three but the template three properties still showing in it..



    enter image description here



    Here first i am filtering the data first and ignoring the duplicates and each time i am sending the newly selected values alone to the service and fetching the data related to the id element.id.



    And using this.addControls('template_properties') to make open the number of rows, and elements will get patched to the form template_properties.



    this.form.patchValue({
    'template_properties': propertiesArray
    });


    As of now everything working fine..



    The problem actually arise from here:



    If we delete a selected list from dropdown, (say i have selected all three template and i have deleted the template two then that particular template's template_properties needs to get deleted..



    I have tried with (remove)="onRemove($event)" but its not working because while remove data, the (change) function also calls..



    How can i remove the template_properties with this.removeControls('template_properties'); of particular deleted template name in the change event or remove event..



    Remove Function:



      onRemove(e) {
    console.log(e);
    this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
    try {
    if (res.status === "true") {
    for (let i = 0; i < res.data.template_properties.length; i++) {
    this.removeControls('template_properties');
    }
    } else {

    }
    }
    catch (error) {

    }
    })
    }


    Remove Control:



      removeControls(control: string) {
    let array = this.form.get(control) as FormArray;
    console.log(array)
    array.removeAt(array.length);
    }


    console.log(array) gives,



    enter image description here










    share|improve this question



























      0












      0








      0


      0






      I am making angular application with angular dynamic form where i am using ng-select library.



      The HTML with select:



          <div *ngFor="let question of questions" class="form-row {{question.class}}">
      <ng-container *ngIf="question.children">
      <div [formArrayName]="question.key" class="w-100">
      <div *ngFor="let item of form.get(question.key).controls; let i=index" [formGroupName]="i" class="row mt-1">
      <div *ngFor="let item of question.children" class="{{item.class}} align-middle">
      <div class="w-100">
      <dynamic-form-builder [question]="item" [index]="i" [form]="form.get(question.key).at(i)"></dynamic-form-builder>
      </div>
      </div>
      </div>
      <div class="container">
      <div class="row">
      <div class="col-6 col-sm-12 col-lg-6 col-md-6">
      <div class="form-label-group" *ngIf="showTemplateDropdown">
      <ngi-select placeholder="Select Template" [required]="true" [hideSelected]="false" [multiple]="true" [items]="templateList"
      dropdownPosition="down" bindLabel="name" bindValue="id" (add)="getTemplateValues($event)" (remove)="onRemove($event)">
      </ngi-select>
      </div>
      </div>
      </div>
      </div>
      <div class="container">
      <div class="row">
      <div class="col-6 col-sm-12 col-lg-6 col-md-6">

      </div>
      <div class="col-6 col-sm-12 col-lg-6 col-md-6 text-right">
      <div class="btn-group float-right">
      <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
      (click)="addControls('template_properties')">
      <span class="mat-button-wrapper"><i class="material-icons mt-2">add</i></span>
      <div class="mat-button-ripple mat-ripple" matripple=""></div>
      <div class="mat-button-focus-overlay"></div>
      </button> &nbsp;&nbsp;&nbsp;
      <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
      (click)="removeControls('template_properties')">
      <span class="mat-button-wrapper"><i class="material-icons mt-2">remove</i></span>
      <div class="mat-button-ripple mat-ripple" matripple=""></div>
      <div class="mat-button-focus-overlay"></div>
      </button>
      </div>
      </div>
      </div>
      </div>

      </div>
      </ng-container>
      <ng-container *ngIf="!question.children">
      <div class="w-100">
      <dynamic-form-builder [question]="question" [form]="form"></dynamic-form-builder>
      </div>
      </ng-container>
      </div>


      Here the [items]="templateList" has the following,



      [{"id":"5bebba2c20ccc52871509d56","name":"Template One"},
      {"id":"5bebba5720ccc52871509d57","name":"Template Two"},
      {"id":"5bebba8d20ccc52871509d5d","name":"Template Three"}]


      I am having (change)="getTemplateValues($event)" event for detecting each change happen when we select an item from dropdown.



      The change event function Edited,



          getTemplateValues(e) {
      this.dynamicFormService.getRest("url" + '/' + e.id").subscribe(res => {
      try {
      if (res.status === "true") {
      res.data.template_properties.forEach(element => {
      this.templateArray.push(element);
      });
      this.form = this.qcs.toFormGroup(this.questions);
      for (let i = 0; i < this.templateArray.length; i++) {
      this.addControls('template_properties');
      }
      let propertiesArray = ;
      this.templateArray.forEach(element => {
      propertiesArray.push(element);
      });
      this.form.patchValue({
      'template_properties': propertiesArray
      });
      } else {

      }
      }
      catch (error) {

      }
      })


      }



      console.log(this.propertiesArray) gives the following,



      [{"property_name":"Property one","property_type":4,"property_required":true,"property_origin":1},{"property_name":"Property one","property_type":5,"property_required":true,"property_origin":1}]  


      In the below image i have deleted template three but the template three properties still showing in it..



      enter image description here



      Here first i am filtering the data first and ignoring the duplicates and each time i am sending the newly selected values alone to the service and fetching the data related to the id element.id.



      And using this.addControls('template_properties') to make open the number of rows, and elements will get patched to the form template_properties.



      this.form.patchValue({
      'template_properties': propertiesArray
      });


      As of now everything working fine..



      The problem actually arise from here:



      If we delete a selected list from dropdown, (say i have selected all three template and i have deleted the template two then that particular template's template_properties needs to get deleted..



      I have tried with (remove)="onRemove($event)" but its not working because while remove data, the (change) function also calls..



      How can i remove the template_properties with this.removeControls('template_properties'); of particular deleted template name in the change event or remove event..



      Remove Function:



        onRemove(e) {
      console.log(e);
      this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
      try {
      if (res.status === "true") {
      for (let i = 0; i < res.data.template_properties.length; i++) {
      this.removeControls('template_properties');
      }
      } else {

      }
      }
      catch (error) {

      }
      })
      }


      Remove Control:



        removeControls(control: string) {
      let array = this.form.get(control) as FormArray;
      console.log(array)
      array.removeAt(array.length);
      }


      console.log(array) gives,



      enter image description here










      share|improve this question
















      I am making angular application with angular dynamic form where i am using ng-select library.



      The HTML with select:



          <div *ngFor="let question of questions" class="form-row {{question.class}}">
      <ng-container *ngIf="question.children">
      <div [formArrayName]="question.key" class="w-100">
      <div *ngFor="let item of form.get(question.key).controls; let i=index" [formGroupName]="i" class="row mt-1">
      <div *ngFor="let item of question.children" class="{{item.class}} align-middle">
      <div class="w-100">
      <dynamic-form-builder [question]="item" [index]="i" [form]="form.get(question.key).at(i)"></dynamic-form-builder>
      </div>
      </div>
      </div>
      <div class="container">
      <div class="row">
      <div class="col-6 col-sm-12 col-lg-6 col-md-6">
      <div class="form-label-group" *ngIf="showTemplateDropdown">
      <ngi-select placeholder="Select Template" [required]="true" [hideSelected]="false" [multiple]="true" [items]="templateList"
      dropdownPosition="down" bindLabel="name" bindValue="id" (add)="getTemplateValues($event)" (remove)="onRemove($event)">
      </ngi-select>
      </div>
      </div>
      </div>
      </div>
      <div class="container">
      <div class="row">
      <div class="col-6 col-sm-12 col-lg-6 col-md-6">

      </div>
      <div class="col-6 col-sm-12 col-lg-6 col-md-6 text-right">
      <div class="btn-group float-right">
      <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
      (click)="addControls('template_properties')">
      <span class="mat-button-wrapper"><i class="material-icons mt-2">add</i></span>
      <div class="mat-button-ripple mat-ripple" matripple=""></div>
      <div class="mat-button-focus-overlay"></div>
      </button> &nbsp;&nbsp;&nbsp;
      <button class="btn btn-primary btn-round btn-fab mat-raised-button" mat-min-fab="" mat-raised-button="" type="button"
      (click)="removeControls('template_properties')">
      <span class="mat-button-wrapper"><i class="material-icons mt-2">remove</i></span>
      <div class="mat-button-ripple mat-ripple" matripple=""></div>
      <div class="mat-button-focus-overlay"></div>
      </button>
      </div>
      </div>
      </div>
      </div>

      </div>
      </ng-container>
      <ng-container *ngIf="!question.children">
      <div class="w-100">
      <dynamic-form-builder [question]="question" [form]="form"></dynamic-form-builder>
      </div>
      </ng-container>
      </div>


      Here the [items]="templateList" has the following,



      [{"id":"5bebba2c20ccc52871509d56","name":"Template One"},
      {"id":"5bebba5720ccc52871509d57","name":"Template Two"},
      {"id":"5bebba8d20ccc52871509d5d","name":"Template Three"}]


      I am having (change)="getTemplateValues($event)" event for detecting each change happen when we select an item from dropdown.



      The change event function Edited,



          getTemplateValues(e) {
      this.dynamicFormService.getRest("url" + '/' + e.id").subscribe(res => {
      try {
      if (res.status === "true") {
      res.data.template_properties.forEach(element => {
      this.templateArray.push(element);
      });
      this.form = this.qcs.toFormGroup(this.questions);
      for (let i = 0; i < this.templateArray.length; i++) {
      this.addControls('template_properties');
      }
      let propertiesArray = ;
      this.templateArray.forEach(element => {
      propertiesArray.push(element);
      });
      this.form.patchValue({
      'template_properties': propertiesArray
      });
      } else {

      }
      }
      catch (error) {

      }
      })


      }



      console.log(this.propertiesArray) gives the following,



      [{"property_name":"Property one","property_type":4,"property_required":true,"property_origin":1},{"property_name":"Property one","property_type":5,"property_required":true,"property_origin":1}]  


      In the below image i have deleted template three but the template three properties still showing in it..



      enter image description here



      Here first i am filtering the data first and ignoring the duplicates and each time i am sending the newly selected values alone to the service and fetching the data related to the id element.id.



      And using this.addControls('template_properties') to make open the number of rows, and elements will get patched to the form template_properties.



      this.form.patchValue({
      'template_properties': propertiesArray
      });


      As of now everything working fine..



      The problem actually arise from here:



      If we delete a selected list from dropdown, (say i have selected all three template and i have deleted the template two then that particular template's template_properties needs to get deleted..



      I have tried with (remove)="onRemove($event)" but its not working because while remove data, the (change) function also calls..



      How can i remove the template_properties with this.removeControls('template_properties'); of particular deleted template name in the change event or remove event..



      Remove Function:



        onRemove(e) {
      console.log(e);
      this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
      try {
      if (res.status === "true") {
      for (let i = 0; i < res.data.template_properties.length; i++) {
      this.removeControls('template_properties');
      }
      } else {

      }
      }
      catch (error) {

      }
      })
      }


      Remove Control:



        removeControls(control: string) {
      let array = this.form.get(control) as FormArray;
      console.log(array)
      array.removeAt(array.length);
      }


      console.log(array) gives,



      enter image description here







      javascript angular typescript angular-reactive-forms angular-ngselect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 10:40







      Maniraj from Karur

















      asked Nov 14 '18 at 6:20









      Maniraj from KarurManiraj from Karur

      1,0391133




      1,0391133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It should be pretty easy fix. Use (add) instead of (change) in ngi-select.



           onRemove(e) {
          console.log(e);
          this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
          try {
          if (res.status === "true") {
          this.form = this.qcs.toFormGroup(this.questions);

          // Issue is here, you should remove only specific record
          // which is being passed from function `e`
          for (let i = 0; i < res.data.template_properties.length; i++) {
          this.removeControls('template_properties');
          }

          let propertiesArray = ;
          this.templateArray.forEach(element => {
          propertiesArray.push(element);
          });
          this.form.patchValue({
          'template_properties': propertiesArray
          });
          } else {

          }
          }
          catch (error) {

          }
          })
          }


          Pass the index in removeControls where you want to remove the element from.



            removeControls(control: string, index:number) {
          let array = this.form.get(control) as FormArray;
          console.log(array)
          array.removeAt(index);
          }


          console.log(array) gives,
          enter image description here






          share|improve this answer


























          • If i use like this, then in remove the entire template_properties is getting removed..

            – Maniraj from Karur
            Nov 14 '18 at 7:44











          • It means, the logic of onRemove function is wrong. Share the code of onRemove.

            – Sunil Singh
            Nov 14 '18 at 7:45











          • Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

            – Maniraj from Karur
            Nov 14 '18 at 7:48













          • Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

            – Maniraj from Karur
            Nov 14 '18 at 8:28











          • Updated the answer where could be the issue. Share your removeControls code as well.

            – Sunil Singh
            Nov 14 '18 at 8: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%2f53294198%2fhow-to-remove-the-child-when-removing-the-parent%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









          0














          It should be pretty easy fix. Use (add) instead of (change) in ngi-select.



           onRemove(e) {
          console.log(e);
          this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
          try {
          if (res.status === "true") {
          this.form = this.qcs.toFormGroup(this.questions);

          // Issue is here, you should remove only specific record
          // which is being passed from function `e`
          for (let i = 0; i < res.data.template_properties.length; i++) {
          this.removeControls('template_properties');
          }

          let propertiesArray = ;
          this.templateArray.forEach(element => {
          propertiesArray.push(element);
          });
          this.form.patchValue({
          'template_properties': propertiesArray
          });
          } else {

          }
          }
          catch (error) {

          }
          })
          }


          Pass the index in removeControls where you want to remove the element from.



            removeControls(control: string, index:number) {
          let array = this.form.get(control) as FormArray;
          console.log(array)
          array.removeAt(index);
          }


          console.log(array) gives,
          enter image description here






          share|improve this answer


























          • If i use like this, then in remove the entire template_properties is getting removed..

            – Maniraj from Karur
            Nov 14 '18 at 7:44











          • It means, the logic of onRemove function is wrong. Share the code of onRemove.

            – Sunil Singh
            Nov 14 '18 at 7:45











          • Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

            – Maniraj from Karur
            Nov 14 '18 at 7:48













          • Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

            – Maniraj from Karur
            Nov 14 '18 at 8:28











          • Updated the answer where could be the issue. Share your removeControls code as well.

            – Sunil Singh
            Nov 14 '18 at 8:33
















          0














          It should be pretty easy fix. Use (add) instead of (change) in ngi-select.



           onRemove(e) {
          console.log(e);
          this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
          try {
          if (res.status === "true") {
          this.form = this.qcs.toFormGroup(this.questions);

          // Issue is here, you should remove only specific record
          // which is being passed from function `e`
          for (let i = 0; i < res.data.template_properties.length; i++) {
          this.removeControls('template_properties');
          }

          let propertiesArray = ;
          this.templateArray.forEach(element => {
          propertiesArray.push(element);
          });
          this.form.patchValue({
          'template_properties': propertiesArray
          });
          } else {

          }
          }
          catch (error) {

          }
          })
          }


          Pass the index in removeControls where you want to remove the element from.



            removeControls(control: string, index:number) {
          let array = this.form.get(control) as FormArray;
          console.log(array)
          array.removeAt(index);
          }


          console.log(array) gives,
          enter image description here






          share|improve this answer


























          • If i use like this, then in remove the entire template_properties is getting removed..

            – Maniraj from Karur
            Nov 14 '18 at 7:44











          • It means, the logic of onRemove function is wrong. Share the code of onRemove.

            – Sunil Singh
            Nov 14 '18 at 7:45











          • Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

            – Maniraj from Karur
            Nov 14 '18 at 7:48













          • Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

            – Maniraj from Karur
            Nov 14 '18 at 8:28











          • Updated the answer where could be the issue. Share your removeControls code as well.

            – Sunil Singh
            Nov 14 '18 at 8:33














          0












          0








          0







          It should be pretty easy fix. Use (add) instead of (change) in ngi-select.



           onRemove(e) {
          console.log(e);
          this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
          try {
          if (res.status === "true") {
          this.form = this.qcs.toFormGroup(this.questions);

          // Issue is here, you should remove only specific record
          // which is being passed from function `e`
          for (let i = 0; i < res.data.template_properties.length; i++) {
          this.removeControls('template_properties');
          }

          let propertiesArray = ;
          this.templateArray.forEach(element => {
          propertiesArray.push(element);
          });
          this.form.patchValue({
          'template_properties': propertiesArray
          });
          } else {

          }
          }
          catch (error) {

          }
          })
          }


          Pass the index in removeControls where you want to remove the element from.



            removeControls(control: string, index:number) {
          let array = this.form.get(control) as FormArray;
          console.log(array)
          array.removeAt(index);
          }


          console.log(array) gives,
          enter image description here






          share|improve this answer















          It should be pretty easy fix. Use (add) instead of (change) in ngi-select.



           onRemove(e) {
          console.log(e);
          this.dynamicFormService.getRest("url" + '/' + e.value.id").subscribe(res => {
          try {
          if (res.status === "true") {
          this.form = this.qcs.toFormGroup(this.questions);

          // Issue is here, you should remove only specific record
          // which is being passed from function `e`
          for (let i = 0; i < res.data.template_properties.length; i++) {
          this.removeControls('template_properties');
          }

          let propertiesArray = ;
          this.templateArray.forEach(element => {
          propertiesArray.push(element);
          });
          this.form.patchValue({
          'template_properties': propertiesArray
          });
          } else {

          }
          }
          catch (error) {

          }
          })
          }


          Pass the index in removeControls where you want to remove the element from.



            removeControls(control: string, index:number) {
          let array = this.form.get(control) as FormArray;
          console.log(array)
          array.removeAt(index);
          }


          console.log(array) gives,
          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 10:45









          Maniraj from Karur

          1,0391133




          1,0391133










          answered Nov 14 '18 at 7:27









          Sunil SinghSunil Singh

          6,2772626




          6,2772626













          • If i use like this, then in remove the entire template_properties is getting removed..

            – Maniraj from Karur
            Nov 14 '18 at 7:44











          • It means, the logic of onRemove function is wrong. Share the code of onRemove.

            – Sunil Singh
            Nov 14 '18 at 7:45











          • Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

            – Maniraj from Karur
            Nov 14 '18 at 7:48













          • Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

            – Maniraj from Karur
            Nov 14 '18 at 8:28











          • Updated the answer where could be the issue. Share your removeControls code as well.

            – Sunil Singh
            Nov 14 '18 at 8:33



















          • If i use like this, then in remove the entire template_properties is getting removed..

            – Maniraj from Karur
            Nov 14 '18 at 7:44











          • It means, the logic of onRemove function is wrong. Share the code of onRemove.

            – Sunil Singh
            Nov 14 '18 at 7:45











          • Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

            – Maniraj from Karur
            Nov 14 '18 at 7:48













          • Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

            – Maniraj from Karur
            Nov 14 '18 at 8:28











          • Updated the answer where could be the issue. Share your removeControls code as well.

            – Sunil Singh
            Nov 14 '18 at 8:33

















          If i use like this, then in remove the entire template_properties is getting removed..

          – Maniraj from Karur
          Nov 14 '18 at 7:44





          If i use like this, then in remove the entire template_properties is getting removed..

          – Maniraj from Karur
          Nov 14 '18 at 7:44













          It means, the logic of onRemove function is wrong. Share the code of onRemove.

          – Sunil Singh
          Nov 14 '18 at 7:45





          It means, the logic of onRemove function is wrong. Share the code of onRemove.

          – Sunil Singh
          Nov 14 '18 at 7:45













          Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

          – Maniraj from Karur
          Nov 14 '18 at 7:48







          Kindly see my updated code for remove.. If i am wrong kindly make me correct for remove function.. How to remove the removed item's (template name) template_properties..

          – Maniraj from Karur
          Nov 14 '18 at 7:48















          Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

          – Maniraj from Karur
          Nov 14 '18 at 8:28





          Sunil, Also updated my getTemplateValues (add) function by removing the filter which shows error after implementing (add) .. Even now Its working for add but not for remove.. Kindly check whether removeControl('template_properties') is right one please..

          – Maniraj from Karur
          Nov 14 '18 at 8:28













          Updated the answer where could be the issue. Share your removeControls code as well.

          – Sunil Singh
          Nov 14 '18 at 8:33





          Updated the answer where could be the issue. Share your removeControls code as well.

          – Sunil Singh
          Nov 14 '18 at 8: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%2f53294198%2fhow-to-remove-the-child-when-removing-the-parent%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma