Skip to content

Instantly share code, notes, and snippets.

@vlio20
Forked from johnlindquist/index.html
Created April 1, 2016 09:48
Show Gist options
  • Select an option

  • Save vlio20/30e66fd12bf49d143957b4d6d742bd7b to your computer and use it in GitHub Desktop.

Select an option

Save vlio20/30e66fd12bf49d143957b4d6d742bd7b to your computer and use it in GitHub Desktop.

Revisions

  1. vlio20 revised this gist Apr 1, 2016. No changes.
  2. @johnlindquist johnlindquist created this gist Mar 31, 2016.
    35 changes: 35 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>RxJSCraft</title>
    <script src="https://npmcdn.com/angular2/bundles/angular2-polyfills.js"></script>
    <script src="https://npmcdn.com/systemjs@0.19.24/dist/system.js"></script>
    <script src="https://npmcdn.com/typescript@1.8.9/lib/typescript.js"></script>
    </head>
    <body>

    <app></app>


    <script>
    SystemJS.config({
    transpiler: "typescript",
    typescriptOptions: {
    emitDecoratorMetadata: true
    },
    map:{
    rxjs: 'https://npmcdn.com/rxjs',
    angular2: 'https://npmcdn.com/angular2'
    },
    packages: {
    "src": {
    "main": "main",
    "defaultExtension": "ts"
    }
    }
    });
    System.import('src');
    </script>
    </body>
    </html>
    43 changes: 43 additions & 0 deletions src\app.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import {Component, DynamicComponentLoader, ElementRef, Injector} from 'angular2/core'
    import {Observable} from 'rxjs/Observable';
    import {Subject} from 'rxjs/Subject';
    import 'rxjs/add/observable/fromPromise';
    import 'rxjs/add/observable/interval';
    import 'rxjs/add/operator/switchMap';


    @Component({
    selector: 'app',
    template: `
    <div>
    <button (click)="click$.next('person')">Add a Person</button>
    <button (click)="click$.next('car')">Add a Car</button>
    <div #loadTarget></div>
    </div>
    `
    })
    export class App {
    click$ = new Subject();

    constructor(
    private _loader:DynamicComponentLoader,
    private _ref:ElementRef,
    private injector:Injector
    ) {
    const loadComp = comp => Observable
    .fromPromise(
    this._loader.loadIntoLocation(
    //the injector looks up the component by string
    this.injector.get(comp),
    this._ref,
    'loadTarget'
    )
    );

    this.click$
    //pass the 'string' from the click to the loadComp
    .switchMap(compName => loadComp(compName))
    .subscribe(comp => comp.instance.id = Math.random());
    }
    }
    12 changes: 12 additions & 0 deletions src\car.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import {Component, Input} from 'angular2/core';

    @Component({
    selector: 'car',
    template: `
    <style>div{border: 2px dashed red}</style>
    <div>Car's id: {{id}}</div>
    `,
    })
    export class Car {
    @Input() id;
    }
    11 changes: 11 additions & 0 deletions src\main.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    import {bootstrap} from 'angular2/platform/browser';
    import {provide} from 'angular2/core';
    import {Person} from './person';
    import {Car} from './car';
    import {App} from './app';

    bootstrap(App, [
    provide('person', {useValue: Person}),
    provide('car', {useValue: Car})
    ])
    .catch(console.log.bind(console));
    12 changes: 12 additions & 0 deletions src\person.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import {Component, Input} from 'angular2/core';

    @Component({
    selector: 'person',
    template: `
    <style>div{border: 2px solid blue}</style>
    <div>My id: {{id}}</div>
    `,
    })
    export class Person {
    @Input() id;
    }