Here I want to display list of customers for that I am writing one services class in that class writing one function getCustomer()
data.service.ts
// @Injectable() // comment to this line in service if we are inject service in component
export class DataService {
constructor(public http: Http) { }
getCustomers(): Promise<ICustomer[]> {
return this.http.get(this.customersBaseUrl)
.toPromise()
.then(response => response.json().data as ICustomer[])
.catch(this.handleError);
}
}
In customer.component.ts call getCustomer()
constructor(@Inject(DataService) private dataService: DataService) { }
ngOnInit() {
this.title;
this.filterText = 'Filter Customers:';
this.dataService.getCustomers().then(customers => this.customers = customers.slice(1, 5));
}
app.module.ts
import { HttpModule } from '@angular/http';
@NgModule({
declarations: [
AppComponent,
CustomersComponent,
],
imports: [
HttpModule,
app_routing,
..
],
providers: [
DataService,
],
bootstrap: [AppComponent,NavbarComponent]
})
Give me suggestion if i am wrong in any point.I am new in typescript so I don't have that much idea about this.Thanks in advance