简体   繁体   中英

Angular undefined error when assigning array to another array

So in this component, i am getting an array of objects from the parent component, which i use to display a list of objects, but when i create a new array, and assign the first array to it and try to work with it, i get an error that the new array is undefined, here's my code:

export class AdherentsListComponent implements OnInit {
  // adherents: Adherent[];
  adherent: Adherent;

  @Input() adherents: Adherent[];

 
  constructor(private adherentService: AdherentService, private alertify: AlertifyService, private router: Router,private modalService: NgbModal) { }

  ngOnInit() {
  }
  
   tempAdherents = this.adherents; //this is what's not working

   loadAdherent(id){
    this.adherentService.getAdherent(id).subscribe((adherent: Adherent) => {
      this.adherent = adherent;
    }, error => {
      this.alertify.error(error);
    })
  }

}

Please any idea how can i create the new array, Thank you.

Try to initzialize your input like @Input() adherents: Adherent[] = [] as Adherent[];

You are missing a declaration for tempAdherents .

export class AdherentsListComponent implements OnInit {
  // adherents: Adherent[];
  adherent: Adherent;

  @Input() adherents: Adherent[];

 
  constructor(private adherentService: AdherentService, private alertify: AlertifyService, private router: Router,private modalService: NgbModal) { }

  ngOnInit() {
  }
  
   var tempAdherents = this.adherents; //this is what's not working

   loadAdherent(id){
    this.adherentService.getAdherent(id).subscribe((adherent: Adherent) => {
      this.adherent = adherent;
    }, error => {
      this.alertify.error(error);
    })
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM