@LeeGenD
import {Directive} from '@angular/core';
@Directive({
selector: 'my-directive',// 选择器名.class/[attr]/div
})
export class MyDirective { }
<p glory="yellow">Knight</p>
<p glory="black">Kngiht</p>
<p *ngIf="true">Knight</p>
<p *ngIf="false">Kngiht</p>
Knight
Ng家族
import {Component} from '@angular/core';
@Component({
/* 和directive相同的属性 */
selector: 'my-component',
styles: ['p {color:white;}'],
template: '<p>Hello World</p>'
})
export class MyComponent { }
<!-- 调用bat-man组件 -->
<bat-man></bat-man>
import {Component} from '@angular/core';
@Component({
/* 和directive相同的属性 */
selector: 'bat-man',
styles: ['/** 一顿乱调 */'],
template: `
<div bat-suit>
<header>Bruce Wayne</header>
<div>body</div>
<footer>foot</footer>
</div>
`
})
export class MyComponent { }
<!-- 调用bat-man组件 -->
<bat-man></bat-man>
转换
import {Component} from '@angular/core';
@Component({
/* 和directive相同的属性 */
selector: 'bat-man',
styles: ['/** 一顿乱调 */'],
template: `
<div bat-suit>
<header>Bruce Wayne</header>
<div>body</div>
<footer>foot</footer>
</div>
`
})
export class MyComponent { }
<!-- 调用bat-man组件 -->
<bat-man></bat-man>
转换
生成
加特效
import {Component} from '@angular/core';
@Component({
selector: 'my-component',
template: `
<h1>Secret Information</h1>
<p *ngIf="isShow">It's *** who have done this to you!</p>
<ng-content></ng-content> <!-- 内容投影 -->
`,
// templateUrl: './my-component.component.html' // 外部引用方式
})
export class MyComponent { }
<!-- my-component.template -->
<h1>Secret Information</h1>
<p *ngIf="isShow">It's *** who have done this to you!</p>
<ng-content></ng-content> <!-- 内容投影 -->
<my-component>Hello World</my-component>
<!-- my-component.template最终生成 -->
<h1>Secret Information</h1>
<p *ngIf="isShow">It's *** who have done this to you!</p>
Hello World <!-- 内容投影被替换 -->
初始化数据
构建content元素(内容投影)
构建view元素(template)
class ParentElementComponent {
constructor() {}
ngOnChanges(changes: any) {}// 当被绑定的输入属性(@Input)的值发生变化时调用
ngOnInit() {}// 初始化指令/组件,仅一次(重要!)
ngDoCheck() {}// 在每个Angular变更检测周期中调用
ngAfterContentInit() {}// 当把内容投影进组件之后调用,仅一次
ngAfterContentChecked() {}// 检测内容投影的变化后调用
ngAfterViewInit() {}// 初始化完组件视图及其子视图之后调用,仅一次
ngAfterViewChecked() {}// 检测视图及其子视图的变化后调用
ngOnDestroy() {}// 销毁时调用
}
// 模板内联
@Component({
selector: 'hero-app',
template: `
<h1>Tour of Heroes</h1>
<style>
span { border: 1px solid black;}
</style>`
})
// 设置styles或styleUrls元数据
@Component({
selector: 'hero-app',
template: `
<h1>Tour of Heroes</h1>
`,
styles: ['h1 { font-weight: normal; }'],
styleUrls: ['app/style.css']
})
// 宿主上下文选择器
// 若宿主元素的祖先元素中存在类名为theme-light的元素,则生效
:host-context(.theme-light) h2 {
background-color: #eef;
}
<issue-card></issue-card>
<p shiny-text>It's been a long day.</p>
<div class="image-uploader"></div>
<div *ngDouble></div>
<issue-card></issue-card> <!-- Component -->
<p shiny-text>It's been a long day.</p> <!-- Directive -->
<div class="image-uploader"></div> <!-- Component -->
<div *ngDouble></div> <!-- Directive -->
import { Component,
Input } from "@angular/core";
@Component({
template: `
<input [value]="inputVal" />
`
})
class InputComponent {
@Input() inputVal = 'init value';
}
<!-- 插值表达式 -->
<p>{{ name }}</p>
<!-- 属性绑定 -->
<input [value]="firstName">
<!-- 事件绑定 -->
<li (click)="selectHero(hero)"></li>
// 双向绑定内幕
@Component({
template: `
<input [value]="inputVal" (input)="valueChange($event)" />
`
})
class InputComponent {
inputVal = 'init value';
valueChange($event) {
this.inputVal = $event.target.value;
}
}
// 双向绑定内幕
@Component({
template: `
<input
[ngModel]="inputVal"
(ngModelChange)="inputVal=$event">
`
})
class InputComponent {
inputVal = 'init value';
}
ngModel指令通过自己的输入属性ngModel和输出属性ngModelChange隐藏了那些细节。
// 申明LogService服务
@Injectable()
export class LogService {
info(): void {}
error(): void {}
}
// 注入LogService
import {Component} from '@angular/core';
@Component({
template: '<p>Hello World</p>'
})
export class MyComponent {
//1. 在构建函数中注入后,Angular会将其实例化
constructor(private logService: LogService) {}
//2. 调用LogService的方法
print() {
this.logService.info('hello world');
}
}
// 浏览器地址相关
import { Location } from '@angular/common';
// loading框
import { LoadingController } from 'ionic-angular';
// 弹框
import { AlertController } from 'ionic-angular';
export class TestService {
private count = 0;
add() {
this.count++;
}
print() {
console.log(`TestService print ${this.count}`);
}
}
@Component({
selector: 'tomato-item',
providers: []
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
@Component({
selector: 'tomato-item',
providers: [TestService]
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
<tomato-item></tomato-item>
<tomato-item></tomato-item>
<tomato-item></tomato-item>
export class TestService {
private count = 0;
add() {
this.count++;
}
print() {
console.log(`TestService print ${this.count}`);
}
}
@Component({
selector: 'tomato-item',
providers: []
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
@Component({
selector: 'tomato-item',
providers: [TestService]
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
<tomato-item></tomato-item>
<tomato-item></tomato-item>
<tomato-item></tomato-item>
@Component({
selector: 'tomato-item',
providers: []
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
@Component({
selector: 'tomato-item',
providers: [TestService]
})
export class TomatoItemComponent {
constructor(private test: TestService) {
this.test.add();
this.test.print();
}
}
第一次注入TestService时实例化,之后每次注入都会使用同一个TestService
每一次注入TestService都会产生新的实例
<!-- 这段代码运行于2017年7月30日 -->
<p>{{ now | date }}</p>
<p>{{ now | date: "MM/dd/yyyy" }}</p>
<p>{{ now | date: "MM/dd" }}</p>
07/30/2017
07/30
Jul 30, 2017
<!-- 这段代码运行于2017年7月30日 -->
<p>{{ now | date | uppercase }}</p>
Jul 30, 2017
Sun Jul 30 2017 11:03:24...
JUL 30, 2017
date
uppercase
Service:公共服务
Directive:指令
Component:组件
Pipe:管道
子模块
NgModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
// 引入其它子模块
imports: [ BrowserModule ],
// Service提供商
providers: [ Logger ],
// 声明Component,Directive,Pipe
declarations: [ AppComponent,
HighLightDirective,
TipPipe ],
// 导出
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
export const routes: Routes = [
{ path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' },
{ path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
TomatoAddComponent
TomatoItemComponent
TomatoService
add()
getAll()
update()