Guest User

Untitled

a guest
Apr 14th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. // app.config.ts
  2. import {OpaqueToken} from 'angular2/core';
  3.  
  4. export let APP_CONFIG = new OpaqueToken('app.config');
  5.  
  6. export interface Config {
  7. apiEndpoint: string,
  8. title: string
  9. }
  10.  
  11. export const CONFIG:Config = {
  12. apiEndpoint: 'api.heroes.com',
  13. title: 'Dependency Injection'
  14. };
  15.  
  16. // app.component.ts
  17. import {Component, Inject, provide} from 'angular2/core';
  18. import {APP_CONFIG, Config, CONFIG} from './app.config';
  19.  
  20. @Component({
  21. selector: 'my-app',
  22. template: `
  23. <h1> {{title}} </h1>
  24. `,
  25. providers: [
  26. provide(APP_CONFIG, {useValue: CONFIG})
  27. ]
  28. })
  29. export class AppComponent implements NgOnInit {
  30. title:string;
  31. constructor( @Inject(APP_CONFIG) config:Config ) {
  32. this.title = config.title;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment