郭德实
@limerick
Angular X
< 英文叫 angular bracket.
Browser support of Angular2
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser;
import { HelloWorldComponent } from './hello-world.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ HelloWorldComponent ],
bootstrap: [ HelloWorldComponent ]
})
export class AppModule {}
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
index.html
<body>
<my-app>Loading AppComponent content here ...</my-app>
</body>
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
title = 'App Component';
}
animations | 动画列表 |
changeDetection | 变化检测策略 |
encapsulation | 样式封装策略 |
host | 元素映射到host |
inputs | 输入 |
outputs | 可订阅的输出 |
viewProviders | 本身及子视图可用provider |
... | ... |
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" class="col-1-4">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</a>
</div>
<hero-search></hero-search>
data -> view | {{expression}},[target] = "expression",bind-target = "expression" |
view -> data | (target) = "statement",on-target = "statement |
data <-> view | [(target)] = "expression",bindon-target = "expression" |
import { Component } from '@angular/core';
@Component({
selector: 'hero-birthday',
template: `The hero's birthday is {{ birthday | date:"MM/dd/yy" }}
`
})
export class HeroBirthdayComponent {
birthday = new Date(1988, 3, 15); // 04/15/88
}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'exponentialStrength'})
export class ExponentialStrengthPipe implements PipeTransform {
transform(value: number, exponent: string): number {
let exp = parseFloat(exponent);
return Math.pow(value, isNaN(exp) ? 1 : exp);
}
}
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
constructor(el: ElementRef, renderer: Renderer) {
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'yellow');
}
}
不使用 DI
export class DashboardComponent implements OnInit {
heroes: Hero[] = [];
heroService: HeroService
constructor(private heroService: HeroService) {
this.heroService = new HeroService();
}
ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes.slice(1, 5));
}
}
使用 DI
export class DashboardComponent implements OnInit {
heroes: Hero[] = [];
/**
* ts通过构造函数参数注入
* 当然前提是 HeroService 是 Injectable,并且已经被注册
**/
constructor(private heroService: HeroService) { }
ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes.slice(1, 5));
}
}
export class PeekABoo implements OnInit {
constructor(private logger: LoggerService) { }
// 实现 OnInit 接口的 `ngOnInit` 方法
ngOnInit() { this.logIt(`OnInit`); }
logIt(msg: string) {
this.logger.log(`#${nextId++} ${msg}`);
}
}
ngZone
function foo() {}
function main() {
foo()
setTimeout(() => console.log('Async task'),2000)
}
var myZoneSpec = {
beforeTask: function () {
console.log('Before task');
},
afterTask: function () {
console.log('After task');
}
};
var myZone = zone.fork(myZoneSpec);
myZone.run(main);
// Logs:
// Before task
// After task
// Before task
// Async task
// After task
为何使用 AoT ?
开发使用 JiT, 生产使用 AoT