Book Image

Angular Services

Book Image

Angular Services

Overview of this book

A primary concern with modern day applications is that they need to be dynamic, and for that, data access from the server side, data authentication, and security are very important. Angular leverages its services to create such state-of-the-art dynamic applications. This book will help you create and design customized services, integrate them into your applications, import third-party plugins, and make your apps perform better and faster. This book starts with a basic rundown on how you can create your own Angular development environment compatible with v2 and v4. You will then use Bootstrap and Angular UI components to create pages. You will also understand how to use controllers to collect data and populate them into NG UIs. Later, you will then create a rating service to evaluate entries and assign a score to them. Next, you will create "cron jobs" in NG. We will then create a crawler service to find all relevant resources regarding a selected headline and generate reports on it. Finally, you will create a service to manage accuracy and provide feedback about troubled areas in the app created. This book is up to date for the 2.4 release and is compatible with the 4.0 release as well, and it does not have any code based on the beta or release candidates.
Table of Contents (15 chapters)
Angular Services
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

The Dependency Injection


Having a service in place, now we can delegate the news assignment task to it. To do so, first import the service class into the component class:

// app/collector/collector.component.ts 
import {Component} from '@angular/core'; 
import {CollectorService} from './collector.service'; 
//... 

Now we need to create that service and pass it to this component. Technically, we don't need to do it ourselves. There is a framework called Dependency Injection and it is in charge of instantiating that service for us.

The Dependency Injection framework looks for all dependencies for a particular class and automatically initializes--injects--them all. All the Dependency Injection framework needs to know is where that service lives. In other words, it needs the name of the service provider.

We can inform the Dependency Injection framework about the service(s) we are interested in via providers metadata as follows:

// app/collector/collector.component.ts 
//... 
@Component({ 
  selector...