import { Component } from '@angular/core';
import { DataService } from './data.service';

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html'
})
export class AppComponent {
  
  constructor(private dataService: DataService) {}
}

/*
Copyright 2016 thoughtram GmbH. All Rights Reserved.
Use of this source code is governed by an TTML-style license that
can be found in the license.txt file at http://thoughtram.io/license.txt
*/
import { platformBrowserDynamic }    from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

/*
Copyright 2016 thoughtram GmbH. All Rights Reserved.
Use of this source code is governed by an TTML-style license that
can be found in the license.txt file at http://thoughtram.io/license.txt
*/
<!DOCTYPE html>
<html>
  <head>
    <title>Angular 2 Demo - Bypassing Providers by thoughtram</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="https://npmcdn.com/core-js/client/shim.min.js"></script>

    <script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
    <script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
    <script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>

    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
    <style>
      .box {
        background: lightblue;
        border: 1px solid #333;
        padding: 1em;
        margin: 1em;
      }
      
      .root { background: lightblue; }
      
      .child {
        background: #628692;
      }
    </style>
  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>
(function(global) {

  var ngVer = '@2.0.0-rc.5'; // lock in the angular package version; do not let it float to current!
  var routerVer = '@3.0.0-rc.1'; // lock router version
  var formsVer = '@0.3.0'; // lock forms version
  var routerDeprecatedVer = '@2.0.0-rc.2'; // temporarily until we update all the guides

  //map tells the System loader where to look for things
  var  map = {
    'app':                        'app',

    '@angular':                   'https://npmcdn.com/@angular', // sufficient if we didn't pin the version
    '@angular/router':            'https://npmcdn.com/@angular/router' + routerVer,
    '@angular/forms':             'https://npmcdn.com/@angular/forms' + formsVer,
    '@angular/router-deprecated': 'https://npmcdn.com/@angular/router-deprecated' + routerDeprecatedVer,
    'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
    'rxjs':                       'https://npmcdn.com/rxjs@5.0.0-beta.6',
    'ts':                         'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
    'typescript':                 'https://npmcdn.com/typescript@1.9.0-dev.20160409/lib/typescript.js',
 };

  //packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.ts',  defaultExtension: 'ts' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
  };

  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'upgrade',
  ];

  // Add map entries for each angular package
  // only because we're pinning the version with `ngVer`.
  ngPackageNames.forEach(function(pkgName) {
    map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
  });

  // Add package entries for angular packages
  ngPackageNames.forEach(function(pkgName) {

    // Bundled (~40 requests):
    packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };

    // Individual files (~300 requests):
    //packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
  });

  // No umd for router yet
  packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };

  // Forms not on rc yet
  packages['@angular/forms'] = { main: 'index.js', defaultExtension: 'js' };

  // Temporarily until we update the guides
  packages['@angular/router-deprecated'] = { main: '/bundles/router-deprecated' + '.umd.js', defaultExtension: 'js' };

  var config = {
    // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      tsconfig: true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    map: map,
    packages: packages
  };

  System.config(config);

})(this);


/*
Copyright 2016 thoughtram GmbH. All Rights Reserved.
Use of this source code is governed by an TTML-style license that
can be found in the license.txt file at http://thoughtram.io/license.txt
*/
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}
<h2>Angular 2 Demo - Bypassing Providers in Angular 2</h2>
    
<p>In this demo we show how to bypass a provider in the injector tree, to get a dependency
from a higher level (not necessarily the nearest provider).</p>

<p>Note how <code>GrandChildComponent</code> gets the dependency from the <code>NgModule</code> provider,
even though there's another provider for the same service in the tree.</p>
<hr>
<br>

<div class="box" [ngClass]="dataService.level">
  DataService provided by: <strong>{{dataService.label}}</strong>
  
  <child-component></child-component>
</div>
<hr>
<center>Brought to you by Team <a href="http://thoughtram.io">thoughtram</a></center>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChildComponent } from './child.component';
import { GrandChildComponent } from './grand-child.component';
import { DataService } from './data.service';
import { ROOT_DATASERVICE } from './app.tokens';

@NgModule({
  imports: [BrowserModule],
  declarations: [AppComponent, ChildComponent, GrandChildComponent],
  bootstrap: [AppComponent],
  providers: [
    DataService,
    { provide: ROOT_DATASERVICE, useExisting: DataService }
  ]
})
export class AppModule {

}

/*
Copyright 2016 thoughtram GmbH. All Rights Reserved.
Use of this source code is governed by an TTML-style license that
can be found in the license.txt file at http://thoughtram.io/license.txt
*/
export class DataService {
  level = 'root';
  label = 'NgModule Provider';
}

export class ChildDataService {
  level = 'child';
  label = 'ChildComponent Provider'
}
import { Component } from '@angular/core';
import { DataService, ChildDataService } from './data.service';

@Component({
  selector: 'child-component',
  template: `
    <div class="box" [ngClass]="dataService.level">
      DataService provided by: <strong>{{dataService.label}}</strong>
      <grand-child-component></grand-child-component>
    </div>
  `,
  providers: [
    { provide: DataService, useClass: ChildDataService }
    // Remove comments to use root DataService instance
    // { provide: DataService, useClass: DataService }
  ]
})
export class ChildComponent {
   
  constructor(private dataService: DataService) {}
}
import { Component, Inject } from '@angular/core';
import { ROOT_DATASERVICE } from './app.tokens';

@Component({
  selector: 'grand-child-component',
  template: `
    <div class="box" [ngClass]="dataService.level">
      DataService provided by: <strong>{{dataService.label}}</strong>
    </div>
  `
})
export class GrandChildComponent {
  
  constructor (@Inject(ROOT_DATASERVICE) private dataService) {}
}
import { OpaqueToken } from '@angular/core';

export const ROOT_DATASERVICE = new OpaqueToken('DataService');