Last active
August 1, 2021 11:27
-
-
Save ierhalim/0484491c96d198b0aa57c039c1f7c83d to your computer and use it in GitHub Desktop.
PrimeNG Dropdown With Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p-dropdown | |
[options]="employees$ | async" | |
placeholder="Choose an employee" | |
[showClear]="true" | |
[filter]="true" | |
></p-dropdown> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { SelectItem } from 'primeng/api'; | |
import { Observable } from 'rxjs'; | |
import { EmployeeService } from './app.service'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'], | |
}) | |
export class AppComponent implements OnInit { | |
constructor(private readonly employeeService: EmployeeService) {} | |
employees$: Observable<Array<SelectItem<number>>>; | |
ngOnInit() { | |
this.employees$ = this.employeeService.getEmployeesAsSelectList$(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpClient } from '@angular/common/http'; | |
import { Injectable } from '@angular/core'; | |
import { SelectItem } from 'primeng/api'; | |
import { Observable } from 'rxjs'; | |
import { map } from 'rxjs/operators'; | |
import { EmployeeModel } from './employee.model'; | |
@Injectable({ providedIn: 'root' }) | |
export class EmployeeService { | |
constructor(private readonly httpClient: HttpClient) {} | |
getEmployeesAsSelectList$(): Observable<Array<SelectItem<number>>>{ | |
return this.httpClient.get<Array<EmployeeModel>>('https://my.api.mockaroo.com/employees.json?key=35003520') | |
.pipe( | |
map((rawData) => { | |
return rawData.map((employe) => ({ | |
value: employe.id, | |
label: `${employe.firstName} ${employe.lastName}`, | |
})); | |
}) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface EmployeeModel{ | |
id: number; | |
firstName: string; | |
lastName: string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment