Formation Angular Lab 3 Services: Lab 3.1: Create Simple Provider
Formation Angular Lab 3 Services: Lab 3.1: Create Simple Provider
Lab 3 Services
Lab 3.1: Create simple provider
1. Build the app using the CLI: Use the following command:
ng new
dependency-injection-ex100 --inline-template --inline-style
ng serve
3.Open app: Open a web browser and navigate to localhost:4200. You should
see “app works!”
@Injectable()
export class CarService {
constructor(){
console.log('CarService: constructor');
}
// Some dummy method.
isSuperCharged(car: string){
return car === 'Ford GT' ? 'yes' : 'no';
}
}
@Component({
selector: 'app-root',
template: `
<car name="Ford GT"></car>
<car name="Corvette Z06"></car>
`,
styles: []
})
• The car service outputs a log when in the constructor. This service
contains a method isSuperCharged that receives a car name as an
argument and returns a yes or no accordingly, as shown in Figure below.
• The app component has a car component that’s used twice. The
car
component specifies the car service as a provider. The car component
invokes the service method isSuperCharged in the method ngOnInit.
ngOnInit is fired when the component has been initialized.
Why do multiple instances of the same service get created? Open the
console and you’ll see something like this :
As you can see, the constructor is invoked twice, as the service is created
twice. That’s because the CarService is provided in the car component and
the car component is create twice.
7. Remove provider from components and add it in the module.
1. Build the app using the CLI: Use the following command:
ng new
dependency-injection-ex350 --inline-template --inline-style
ng serve
5.Open app: Open a web browser and navigate to localhost:4200. You should
see “app works!”
getTime(): string {
return new Date() + "";
}
}
@Component({
selector: 'app-root',
template: `
<h1>
{{watch.getTime()}}
</h1>
`,
styles: [],
providers: [{
provide: Watch,
useClass: Seiko
}]
})
export class AppComponent {
constructor(private watch:Watch){}
}
Your app should be working at localhost:4200. Note that when we use the
Provider element of the @Component annotation to create the dependency,
we specify that subclass of the Watch (a Seiko).
1. Build the app using the CLI: Use the following command:
cd dependency-injection-ex400
ng serve
@Injectable()
export class LoggingService {
constructor(private dateAndTime: boolean){
console.log('LoggingService: constructor');
}
log(message){
console.log((this.dateAndTime ? new Date() + ': ' : '') +
message);
}
}
@Component({
selector: 'app-root',
template: `
<h1>
{{title}}
</h1>
`,
styles: [],
providers: [provideLoggingService()]
})
export class AppComponent {
constructor(private logging: LoggingService){
logging.log('test log');
}
title = 'app works!';
}
export const LOGGING_USE_DATE = false;
export function provideLoggingService() {
return {
provide: LoggingService,
useFactory: () => new LoggingService(LOGGING_USE_DATE)
}}
Your app should be working at localhost:4200. Note that this logging
service has the option of including the logging date and time. You can set
this using the constructor
to the logging service. A factory provider is
used to provide an instance of the logging service.
Date not included in logging
Lab 3.3: Value provider
Value providers simply provide a value of an object, as shown below
1. Build the app using the CLI: Use the following command:
ng new
dependency-injection-ex600 --inline-template --inline-style
ng serve
7.Open app: Open a web browser and navigate to localhost:4200. You should
see “app works!”
@Component({
selector: 'app-root',
template: `
<h1>
{{title}}
</h1>
`,
styles: [],
providers: [{
provide: 'language',
useValue: 'en'
}]
})
export class AppComponent {
title: string = '';
constructor(private injector: Injector){
this.title = 'Language is: ' + injector.get('language');
}
}