<!DOCTYPE html>
<html>
<head>
<title>Angular 2 Services - Modern JS</title>
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.8"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app').then(null, console.error.bind(console));
</script>
</head>
<body>
<banner-app>Loading...</banner-app>
</body>
</html>
import {platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {BannerModule} from './banner.module';
platformBrowserDynamic().bootstrapModule(BannerModule);
import {Component} from '@angular/core';
import {BannerService} from './banner.service';
@Component({
selector: 'banner-app',
templateUrl:'app/banner.template.html',
providers: [BannerService]
})
export class Banner implements OnInit {
constructor(private _bannerService: BannerService) { }
message: string;
isTheBannerVisible: boolean = true;
getMessage() {
this._bannerService.getMessage().then(message => this.message = message);
}
ngOnInit() {
this.getMessage();
}
handleClick(event) {
this.isTheBannerVisible = !this.isTheBannerVisible;
}
handleAjaxButtonClick(event) {
this._bannerService.getMessageViaAjax()
.subscribe(
message => this.message = message);
}
}
import {Injectable} from '@angular/core';
import {Http,Response} from '@angular/http';
import {BANNER_MESSAGE} from './mock-banner';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
@Injectable()
export class BannerService {
constructor (private http: Http) {}
getMessage() {
return Promise.resolve(BANNER_MESSAGE);
}
getMessageViaAjax(): Observable<string> {
return this.http.get('https://jsonplaceholder.herokuapp.com/posts/1')
.map(res => res.json().title);
}
}
export var BANNER_MESSAGE: string = 'Angular 101!';
<div>
<h4><button (click)="handleClick()">Click to {{isTheBannerVisible ? 'hide' : 'show'}} {{message}}</button></h4>
<div [hidden]="!isTheBannerVisible">{{message}}</div>
<h4><button (click)="handleAjaxButtonClick()">Click to update message via AJAX</button></h4>
</div>
System.config({
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@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',
// other libraries
'rxjs': 'npm:rxjs',
'ts': 'npm:plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'npm:typescript@2.0.3/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { Banner } from './banner.component';
@NgModule( {
imports: [ BrowserModule, HttpModule],
declarations: [ Banner ],
exports: [ Banner ],
bootstrap: [ Banner ]
} )
export class BannerModule {
}