Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modern JavaScript Applications

You're reading from   Modern JavaScript Applications Keep abreast of the practical uses of modern JavaScript

Arrow left icon
Product type Paperback
Published in Jul 2016
Publisher Packt
ISBN-13 9781785881442
Length 330 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Narayan Prusty Narayan Prusty
Author Profile Icon Narayan Prusty
Narayan Prusty
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Breaking into Microservices Architecture FREE CHAPTER 2. Building a Coupon Site 3. Communication between Browsers in Real Time 4. Building a Chatroulette 5. Bidirectional Communication in Real Time 6. Building a Live Score Site 7. Functional Reactive Programming 8. Building an Advanced Profile Search Widget 9. New Features of Bootstrap 4 10. Building User Interfaces Using React 11. Building an RSS Reader Using React and Flux 12. New Features of Angular 2 13. Building a Search Engine Template Using AngularJS 2 14. Securing and Scaling Node.js Applications Index

Understanding providers


A provider tells Angular 2 how to create an instance of a service while injecting it. A provider is set using the providers or viewProviders properties of a component.

Let's look at an example of how to create providers. Place this code above the App component's code:

var Service1 = ng.core.Class({
  constructor: function() {
  },
  getValue: function() {
    return "xyz"
  }
});

var Service2 = ng.core.Class({
  constructor: function() {
  },
  getValue: function() {
    return "def"
  }
});

var Service3 = ng.core.Class({
  constructor: function() {
  },
  getValue: function() {
    return "mno"
  }
});

var Service4 = ng.core.Class({
  constructor: [Service2, Service3, function(s2, s3) {
    console.log(s2);
    console.log(s3);
  }],
  getValue: function() {
    return "abc"
  }
});

var ServiceTest1 = ng.core.Component({
  selector: "st1",
  viewProviders: [
    ng.core.provide(Service1, {useClass: Service4}),
    ng.core.provide(Service2, {useValue: "def"}),
...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime