What Is Angular 2 Service? (Steps For Create and Use of Angular 2 Services)
What Is Angular 2 Service? (Steps For Create and Use of Angular 2 Services)
What Is Angular 2 Service? (Steps For Create and Use of Angular 2 Services)
After creating user service, I will use the user service “getUsers()” method in the user
component’s ngOnInit() method to load the returns user collections on user screen.
I am also using the REST API Url (https://api.github.com/users/hadley/orgs) and this RESTful API
will returns the users.
app.module.ts :-
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
UserComponent,
HeaderComponent,
MenuComponent,
LoginComponent,
RegistrationComponent
],
imports: [
UniversalModule, // MUST BE FIRST IMPORT. THIS AUTOMATICALLY IMPORTS
BROWSERMODULE, HTTPMODULE, AND JSONPMODULE TOO.
RouterModule.forRoot([ //RouterModule.forRoot method in the module imports to
configure the router.
{ path: '', redirectTo: 'user', pathMatch: 'full' },
{ path: 'user/:id', component: UserComponent }, //HERE ID IS A ROUTE
PARAMETER.
{ path: 'login', component: LoginComponent },
{ path: 'registration', component: RegistrationComponent },
{ path: '**', redirectTo: 'user' }
]),
FormsModule,
ReactiveFormsModule
]
})
export class AppModule {
}
//BEGIN-REGION - USERSERVICE
@Injectable()
export class UserService {
constructor(private _http: Http) { }
getUsers(apiUrl) {
return this._http.get(apiUrl).map((data: Response) => data.json());
}
}
//END BEGIN - USERSERVICE
//BEGIN-REGION - USERCOMPONENT
@Component({
selector: 'user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css'],
providers: [UserService]
})
user.component.html :-
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h2>Angular 2 - User Services</h2>
</div>
<hr />
<div class="ibox-content">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name </th>
<th>Description </th>
<th>URls </th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users; let i = index">
<td>{{user.id}}</td>
<td>{{user.login}}</td>
<td>{{user.description}}</td>
<td><a href="{{user.public_members_url}}"> {{user.public_memb
ers_url}}</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Result :-
Features Of Angular 2 Services [Observables vs. Promises]
What are the features of Angular 2 Service?
The Angular 2 is using services concept and it provide the multiple features to us that are,
1. Services are singleton objects.
2. Services are capable of returning the data in the form promises or observables.
3. Service class is decorated with Injectable decorator.
4. The Injectable decorator is required only if our service class is making use of some Angular
injectable like Http, Response and HttpModule service within it.
The Angular 2 is using services concept and it provide the multiple features to us that are,
1. Services are singleton objects.
2. Services are capable of returning the data in the form promises or observables.
3. Service class is decorated with Injectable decorator.
4. The Injectable decorator is required only if our service class is making use of some Angular
injectable like Http, Response and HttpModule service within it.
Steps for creating an Angular 2 Service:-
There are four steps as,
1. Import the injectable member i.e.
import {Injectable} from '@angular/core';
2. Import the HttpModule, Http and Response members’ i.e.
import { HttpModule, Http, Response } from '@angular/http';
3. Add the @Injectable Decorator i.e. @Injectable()
4. Export to the Service class i.e.