0

Am trying to set value to nested componenet which is added dynamically from json, but I could'nt acheieve it

What I Have Tried

Am creating a nested component inside another component, and setting a json value on a button click, am able to set the value for name, but not for the addressees and extra space object array

please help me to resolve this issue, sorry if this is a duplicated question

Plunker

App.component.html

<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <div class="margin-20">
        <h4>Add customer</h4>
      </div>
      <form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm)">
        <div class="form-group">
          <label>Name</label>
          <input type="text" class="form-control" formControlName="name">
          <small *ngIf="!myForm.controls.name.valid" class="text-danger">
              Name is required (minimum 5 characters).
            </small>
        </div>
        <!--addresses-->
        <div formArrayName="addressees">
          <div *ngFor="let address of myForm.controls.addressees.controls; let i=index" class="panel panel-default">
            <div class="panel-heading">
              <span>Address {{i + 1}}</span>
              <span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.addressees.controls.length > 1" (click)="removeAddress(i)"></span>
            </div>
            <div class="panel-body" [formGroupName]="i">
              <address [group]="myForm.controls.addressees.controls[i]"></address>
            </div>
          </div> 
        </div>

        <div class="margin-20">
          <a (click)="addAddress()" style="cursor: default">
            Add another address +
          </a>
        </div>

        <div class="margin-20">
          <button type="submit" class="btn btn-primary pull-right" [disabled]="!myForm.valid">Submit</button>
        </div>
        <div class="clearfix"></div>

        <div class="margin-20">
          <div>myForm details:-</div>
          <pre>Is myForm valid?: <br>{{myForm.valid | json}}</pre>
          <pre>form value: <br>{{myForm.value | json}}</pre>
        </div>
      </form>
    </div>
  </div>
</div>

App.Component.ts

export class AppComponent implements OnInit {
    public myForm: FormGroup;

    constructor(private _fb: FormBuilder) { }

    ngOnInit() {
        this.myForm = this._fb.group({
            name: ['', [Validators.required, Validators.minLength(5)]],
            addressees: this._fb.array([])
        });

        // add address
        this.addAddress();

        /* subscribe to addresses value changes */
        // this.myForm.controls['addresses'].valueChanges.subscribe(x => {
        //   console.log(x);
        // })
    }

    initAddress() {
        return this._fb.group({
            street: ['', Validators.required],
            postcode: [''],
            extraspaces: this._fb.array([])
        });
    }

    addAddress() {
        const control = <FormArray>this.myForm.controls['addressees'];
        const addrCtrl = this.initAddress();

        control.push(addrCtrl);

        /* subscribe to individual address value changes */
        // addrCtrl.valueChanges.subscribe(x => {
        //   console.log(x);
        // })
    }

    removeAddress(i: number) {
        const control = <FormArray>this.myForm.controls['addressees'];
        control.removeAt(i);
    }

    save(model: Customer) {
        // call API to save
        // ...
        console.log(model);
    }
}
JeanJacques
  • 1,754
  • 1
  • 12
  • 25
balajivaishnav
  • 2,795
  • 4
  • 23
  • 38
  • is there anybody can answer my question – balajivaishnav Jun 14 '17 at 13:34
  • @AJT_82 can you please help me to resolve this issue – balajivaishnav Jun 14 '17 at 13:40
  • @George K can you please help me to resolve this issue – balajivaishnav Jun 14 '17 at 13:41
  • I see no attempt to add any data? :) – AT82 Jun 15 '17 at 09:45
  • @AJT_82 I don't know what happened to my code, the link which I gave is not working too, sorry for that – balajivaishnav Jun 15 '17 at 10:18
  • @AJT_82 my problem is to access the form builder array inside the form builder group so that I can push my form builder array in to form array – balajivaishnav Jun 15 '17 at 10:19
  • Don't worry about the plunker. Angular plunkers are buggy at the moment and do not work. But are you receiving JSON you are trying to display. If so, where is the attempt to do that in your code? :) – AT82 Jun 15 '17 at 10:22
  • @AJT_82 now I have updated the code u can check this link http://plnkr.co/edit/1CBrDauQM9fJneVQjN0t on click of setdata button, I need to set data by creating the components without adding the components manually – balajivaishnav Jun 15 '17 at 10:44
  • @AJT_82 any updates ?.. sorry to bug you – balajivaishnav Jun 15 '17 at 11:39
  • No worries, I will shortly take a look at your code, I'd just wish they'd get the Angular plunkers up and running so we could actually test the code and debug it. This is a type of question that needs debugging, at least for me I need to try it and see where the problem lies :) – AT82 Jun 15 '17 at 11:47
  • @AJT_82 thanks for you reply, will wait no problem – balajivaishnav Jun 15 '17 at 11:52
  • Seems the plunker is still buggy. You can check this answer though, if you can make some headway with the applying of data... it's not child and grandchild, but nested anyway, so it might help even a little on the way, while we wait for plunkers to work :( https://stackoverflow.com/a/44139453/6294072 – AT82 Jun 15 '17 at 15:19

0 Answers0