-1

I have this model

export class model{
  Id: string;
  Name: string;
  home: Home[];
}

export class Home{
  street: string;
  country: string;
  CP: string;
}

the question is how to use the values of model Home in father model and use the values in a form to insert new register for example:

<form>
 <input type="text" ([ngModel])="model.id">
 <input type="text" ([ngModel])="model.Name">
 <input type="text" ([ngModel])="model.Home.CP"><!--How to implements the values of home to insert a value in the BD-->
</form>

thanks

Juan Perez
  • 29
  • 1
  • 5
  • What is a father module? – Igor Aug 21 '18 at 19:12
  • Also casing matters and if you are going to access an item in an array you need to use the array indexer or loop through it and access it multiple times. – Igor Aug 21 '18 at 19:13
  • Possible duplicate of https://stackoverflow.com/questions/43851357/loop-through-array-of-strings-angular2 – Igor Aug 21 '18 at 19:14

1 Answers1

0

@Juan Perez

To use Home[] array in model class, you need to instantiate the array in the model class constructor. Then when binding data to the home property of the model class, you have to use an index in the home array. Possible code would look like below

<input type="text" ([ngModel])="model.home[index].CP">

In the model class the constructor should look like below

export class model{
  Id: string;
  Name: string;
  home: Home[];

  model() {
    home = new Array(3).fill(new Home());
  }
}

export class Home{
  street: string;
  country: string;
  CP: string;

  Home() {
    street = "";
    country = "";
    CP = "";
  }
}
simplelenz
  • 877
  • 1
  • 9
  • 22