<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular2 playground</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* Styles go here */
### Angular Starter Plunker - Typescript
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormBuilder, FormGroup } from '@angular/forms';
import { SharedService } from './shared.service';
import { Sibling1Component } from './sibling1.component';
import { Sibling2Component } from './sibling2.component';
import { ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<div>
<h2>Communication between component siblings via a service</h2>
<a href="https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service" target="_blank">Reference</a>
</div>
Start typing in casenumber field of any component and you
will see the data in other component. These two siblings
exchange the data on submit.
<hr>
<sibling1-component></sibling1-component><hr>
<sibling2-component></sibling2-component>
`,
providers: [SharedService]
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
}
@NgModule({
imports: [ BrowserModule,ReactiveFormsModule ],
declarations: [ App, Sibling1Component, Sibling2Component ],
bootstrap: [ App ]
})
export class AppModule {}
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SharedService {
//private caseNumber: any;
// Observable string sources
private caseNumber = new Subject<string>();
// Observable string streams
caseNumber$ = this.caseNumber.asObservable();
// Service message commands
publishData(data: string) {
this.caseNumber.next(data);
}
// publishData(data: string) {
// console.log('Inside publish data: ' + data);
// this.caseNumber = data;
// }
// subscribeData() {
// console.log('Inside subscribeData: ' + this.caseNumber);
// return this.caseNumber;
// }
}
import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { SharedService } from './shared.service';
@Component({
//moduleId: module.id,
selector: 'sibling1-component',
template: `
<div class="panel-group">
<div class="panel panel-primary">
<div class="panel-heading">Sibling 1</div>
<div class="panel-body">
<div *ngIf="searchCaseNumber">Searching for: {{searchCaseNumber}}</div>
<form [formGroup]="sibling1Form" class="form-inline" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="caseNumber">Case Number:</label>
<input type="text" class="form-control" id="caseNumber" formControlName="caseNumber" placeholder="Case Number">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
`
})
export class Sibling1Component implements OnChanges {
sibling1Form: FormGroup;
searchCaseNumber: any = '';
constructor(private fb: FormBuilder, private _sharedService: SharedService) {
this.createForm();
this._sharedService.caseNumber$.subscribe(
data => {
console.log('Sibling1Component-received from sibling2: ' + data);
this.searchCaseNumber = data;
this.sibling1Form.patchValue({
caseNumber: data
});
});
}
createForm() {
this.sibling1Form = this.fb.group({
caseNumber: ''
});
}
ngOnChanges() {
//this.sibling2Form.get('caseNumber').value=this._sharedService.subscribeData();
// this.searchCaseNumber = this._sharedService.subscribeData();
//console.log('Sibling1Component-received from sibling2: ' + this.searchCaseNumber);
}
onSubmit(): void {
// console.log('Sibling1Component-received from sibling2: ' + this._sharedService.subscribeData());
console.log('Form submitted-sibling1Form');
let caseNumber = this.sibling1Form.get('caseNumber').value;
this.searchCaseNumber = caseNumber;
this._sharedService.publishData(caseNumber);
}
}
import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { SharedService } from './shared.service';
@Component({
//moduleId: module.id,
selector: 'sibling2-component',
template: `
<div class="panel-group">
<div class="panel panel-primary">
<div class="panel-heading">Sibling 2</div>
<div class="panel-body">
<div *ngIf="searchCaseNumber">Searching for: {{searchCaseNumber}}</div>
<form [formGroup]="sibling2Form" class="form-inline" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="caseNumber">Case Number:</label>
<input type="text" class="form-control" id="caseNumber" formControlName="caseNumber" placeholder="Case Number">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
`
})
export class Sibling2Component implements OnChanges {
sibling2Form: FormGroup;
searchCaseNumber: any = '';
constructor(private fb: FormBuilder, private _sharedService: SharedService) {
this.createForm();
this._sharedService.caseNumber$.subscribe(
data => {
console.log('Sibling2Component-received from sibling1: ' + data);
this.searchCaseNumber = data;
this.sibling2Form.patchValue({
caseNumber: data
});
});
}
createForm() {
this.sibling2Form = this.fb.group({
caseNumber: ''
});
}
ngOnChanges() {
//this.sibling2Form.get('caseNumber').value=this._sharedService.subscribeData();
//this.searchCaseNumber = this._sharedService.subscribeData();
//console.log('Sibling2Component-received from sibling1: ' + this.searchCaseNumber);
}
onSubmit(): void {
console.log('Form submitted-sibling2Form');
let caseNumber = this.sibling2Form.get('caseNumber').value;
this.searchCaseNumber = caseNumber;
this._sharedService.publishData(caseNumber);
}
}