Populate Dropdown from enum and set value

UI Components for Angular
Post Reply
sleeper9
Posts: 14
Joined: 29 Mar 2016, 16:37

20 Jun 2016, 17:33

We have an enum like

Code: Select all

export enum Status {
    UNBLOCKED,
    BLOCKED,
    DEFECTIVE
}
Now I'd like to have this enum as a source of options for a Dropdown component. I do it as the following:

Code: Select all

<p-dropdown [options]="statuses" [(ngModel)]="selectedStatus" [autoWidth]="false"></p-dropdown>
Where the statuses and selectedStatus are the following (setupStatuses is initialized properly):

Code: Select all

statuses: Status[];
selectedStatus: number;

...

ngOnInit(): any {
...
    for (let i=0; i<setupStatuses.length; i++) {
        this.statuses.push({label: setupStatuses[i], value: i});
    }
}
Now, the dropdown list is populated correctly, but the selected item does not correspond to the value in the model. E.g. the selected item in the dropdown always will be UNBLOCKED, regardless of the selectedStatus initial value. Besides that, the selectedStatus will take the value of the label of the selected item, not the value e.g. DEFECTIVE. What do I do wrong?

sleeper9
Posts: 14
Joined: 29 Mar 2016, 16:37

20 Jun 2016, 17:51

Nevermind, it was my fault... I misspelled the ngModel name one place.

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 23 guests