angularselectdrop-down-menung-optionsangularjs-ng-options

select ng-model ng-options doesn't work


Could you help me with my problem? I relly don't understand why it doesn't work.

I'd like to dynamically generate select from Level[] from database

<select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

dictionary-item.component.html:

<div class="pull-right level">
    <select id="levelSelect" ng-model="levelModel" ng-options="level as level.level for level in levels" (click)="stopPropagation($event)">
    </select>
  </div>

dictionary-item.component.ts:

    @Component({
      selector: 'app-dictionary-item',
      templateUrl: './dictionary-item.component.html',
      styleUrls: ['./dictionary-item.component.css']
    })
    export class DictionaryItemComponent implements OnInit {
      @Input() dictionaryItem: DictionaryItem;
      levels: Level[] = [];

      constructor(private dictionaryService: DictionaryService) { }

      ngOnInit() {
        this.levels = this.dictionaryService.getLevels();
        console.log(this.levels);
/*
from Chrome console:
(5) [{…}, {…}, {…}, {…}, {…}]
0
:
{id: 1, level: 1}
1
:
{id: 2, level: 2}
2
:
{id: 3, level: 3}
3
:
{id: 4, level: 4}
4
:
{id: 5, level: 5}
length
:
5
__proto__
:
Array(0)
*/
      }
    }

this.dictionaryService.getLevels() returns Level[]:

where Level:

export class Level {
    public id: number;
    public level: number;

    constructor(id: number, level: number) { }
}

Why Angular generates a blank select list on the page? Without any values?

I've even tried to set in component:

ngOnInit() {
    this.levels = [new Level(1,1), new Level(2,2), new Level(3,3)];
  }

but without any generation of on the website. Nothing.

Thank you in advance! Mateusz


Solution

  • It doesn't work because you're not using angularJS.

    <select [(ngModel)]="levelModel" (click)="stopPropagation($event)">
      <option *ngFor="let level of levels" [value]="level">{{ level }}</option>
    </select>