Tuesday, May 1, 2018

Angular - Services

What is a service

Simply put it is a class with a focused purpose. Generally it is not specific to any component, and thus provides logic that can be used for different components. Useful for encapsulating external interactions such as web api calls, etc.

Dependency Injection

We can use Angular and Dependency Injection to inject the service into the component. This makes testing much easier by allowing for Mocks. Luckily, Angular has a built in injector.

In order for a component to use the service it just needs to add a parameter to the constructor of the component.

Example:

my.service.ts

import { Injectable } from '@angular/core'

@Injectable()
export class MyService {
   getMyThings() : IThing[] { ... }
}

export class MyComponent {
    private _myService;
    constructor(myService: MyService) {
        _myService = myService
    }
}

Tip: Here is the same MyComponent class, but in shorter syntax

export class MyComponent {

    constructor(private _myService: MyService) {
    }
}

Registering the Service


A service can be registered at with different scope. Choose the right method to get the right scope.

Method 1: Registering a Provider

To make the service available to a component and its children add it to the array of providers where the component is defined.

import { MyService } from './my.service';

@Component({
    selector: ...,
    template: ..., 
    providers: [MyService]
})
export class MyComponent {
    constructor(private _myService) {}
}



23 comments:

whatsapp plus apk said...

love whatsapp groups

whatsapp plus apk said...

love whatsapp groups

luckys said...

gta 5 apk

Adion said...

gb whatsapp apk 2019

Unknown said...
This comment has been removed by the author.
parvina said...

Just love your article.you might be interested in :how to view insta dp

gbapps said...

A dedication of gratefulness is all together for all the exertion in sharing this dazzling post and I truly to welcome it. Your dedication and reliable work has helped you to pass on dazzling quality articles everything considered


Download Latest Version GBWhatsApp Apk It's One Of The Best Mods Of WhatsApp

Unknown said...

click here

more information

jacobs said...

wow nice post you shared with us. keep posting.
TEKKEN 7 APK+ISO
Tekken 7 APK+ISO. In this game, the fighters fight against the enemies for their survival. The fighters have different controls and fighting styles that help in the battle. Here is the latest 2020 and updated version of taken 7 APK + ISO game. you will like it most.
visit and check

luckys said...

exact age calculator

Ramandeep Singh said...

how to train your dragon 3 free online

Ethan said...

Wonderful post i really thanks for sharing! WhatsApp Plus 2020

Anonymous said...

fuck gbwhatsapp

Ikram Parvez said...

Movierulz has converted to 4Movierulz is that true?

jame said...

use jtwhatsapp and start chatting with your friends.

Nawazz said...

wow fantastic app you can try Easy Flight Tracker Apk

Danny Danials said...

I have been using this app PicLab Mod Apk : and downloaded and playing it regularly.

Raj Maan said...

wow fantastic Post. gb whatsapp

GBWhatsApp Pro said...

Amazzing post i like it this is very usefull for me also check GBWhatsApp ProA

microgapk said...

Microg Download APK

Fastway Tracking said...

Hi Everyone , Track your Parcel and showing a good result,fast way tracking

rahamai said...

Thank you for reading my article! I hope to see more of your work in the future.

Thank you for reading my article! I hope to see more of your work in the future.

oscar clark said...

thanks for sharing info about it.An Angular service is simply a Javascript function. All we need to do is to create a class and add methods & properties. i red about it in a group of gbwhatsapp download 2022