ngFor 它没有 return 任何东西

ngFor it does not return anything

我有一个来自服务的列表,在组件中我试图将它加载到 select-选项中,但它没有给我任何东西。服务方法没问题,我已经检查过了,有没有人看到错误?

这是组件:

public listaAnyos: number;

 ngOnInit() {
    this.service.getAnyos(this.listaAnyos);
  }

这是 HTML:

<select class="form-control" id="anio" [(ngModel)]="anyoSeleccionado" (change)="getControlEstadoMes($event.target.value)">
          <option *ngFor="let anyos of listaAnyos"></option>  
</select>

您的代码有误。

组件

public listaAnyos: number[];

ngOnInit() {
   this.listaAnyos = this.service.getAnyos(); // Assuming that getAnyos() returns array of number(Anyos).
}

HTML

<select class="form-control" id="anio" [(ngModel)]="anyoSeleccionado" (change)="getControlEstadoMes($event.target.value)">
          <option *ngFor="let anyos of listaAnyos">{{anyos}}</option>  
</select>