<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-beta.17/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.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 */
### Angular2 - Typescript - RC.0
>* when to use ChangeDetectionStrategy.OnPush
>* how to trigger the chagnes detect for component with OnPush
>* other approch to triger change detect besides the Input Change
>* note what the 'change' means, it is the reference change, not object chagne
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: "./src",
'@angular': 'https://npmcdn.com/@angular',
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
'@angular/core': {
main: 'bundles/core.umd.js',
defaultExtension: 'js'
},
'@angular/compiler': {
main: 'bundles/compiler.umd.js',
defaultExtension: 'js'
},
'@angular/common': {
main: 'bundles/common.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser-dynamic': {
main: 'bundles/platform-browser-dynamic.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser': {
main: 'bundles/platform-browser.umd.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {App} from './app';
bootstrap(App, [])
.catch(err => console.error(err));
//our root app component
import {Component,ChangeDetectionStrategy,ChangeDetectorRef,Input} from '@angular/core'
@Component({
selector: 'child',
/**********************
OnPush will only trigger change detection
when an input property has been changed(in parent) as
the result of a change detection check./
**********************/
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<div *ngFor="let hero of heros">{{hero}}</div>`
})
class Child {
@Input() heros:Observable<[]>;
constructor(public ref:ChangeDetectorRef){
// change won't be detected,please consider why
setTimeout(()=>{
this.heros = ['Change',"from ","child, wont show"];
},2000);
//
setTimeout(()=>{
this.heros = ['Change',"from ","child, shows up ....yep"];
// CONSIDER why this works
this.ref.markForCheck();
// and this doesn't work
// this.ref.detectChanges();
},5000);
}
}
@Component({
selector: 'my-app',
template: `
<child [heros]="herosArr"><child>
`,
/* here you can't use this strategy ,please consider why*/
// changeDetection: ChangeDetectionStrategy.OnPush,
directives: [Child]
})
export class App {
herosArr:[]=['Init','value']
constructor(){
// change will be detected
setTimeout(()=>{
this.herosArr = ['new ','array','reference','in parent'];
},1000);
// change won't be detected,please consider why
setTimeout(()=>{
this.herosArr.push ['append','something','not reflect on page'];
},3000);
// change will be detected
setTimeout(()=>{
this.herosArr = ['end..........'];
},4000);
}
}