<div *ngIf="isLoggedIn; else loggedOut">
Welcome back, friend.
</div>
<ng-template #loggedOut>
Please friend, login.
</ng-template>
複製代碼
<ng-template [ngIf]="isLoggedIn" [ngIfElse]="loggedOut">
Welcome back, friend.
</ng-template>
<ng-template #loggedOut>
Please friend, login.
</ng-template>
複製代碼
<ng-container *ngIf="isLoggedIn;then loggedIn;loggedOut">
<ng-container>
<ng-template #loggedIn>
Welcome back, friend.
</ng-template>
<ng-template #loggedOut>
Please friend, login.
</ng-template>
複製代碼
<div [hidden]="!isLoggedIn">
Welcome back, friend.
</div>
複製代碼
export const heroDetail = {
bindings: {
hero: '<',
deleted: '&'
},
template: `
<h2>{{$ctrl.hero.name}} details!</h2>
<div><label>id: </label>{{$ctrl.hero.id}}</div>
<button ng-click="$ctrl.onDelete()">Delete</button>
`,
controller: function() {
this.onDelete = () => {
this.deleted(this.hero);
};
}
};
複製代碼
import { Directive, ElementRef, Injector, Input, Output, EventEmitter } from '@angular/core';
import { UpgradeComponent } from '@angular/upgrade/static';
import { Hero } from '../hero';
@Directive({
selector: 'hero-detail'
})
export class HeroDetailDirective extends UpgradeComponent {
# 須要對應於angularjs組件定義的數據綁定
@Input() hero: Hero;
@Output() deleted: EventEmitter<Hero>;
constructor(elementRef: ElementRef, injector: Injector) {
super('heroDetail', elementRef, injector);
}
}
複製代碼
import { Component } from '@angular/core';
import { Hero } from '../hero';
@Component({
selector: 'my-container',
template: `
<h1>Tour of Heroes</h1>
<hero-detail [hero]="hero"
(deleted)="heroDeleted($event)">
</hero-detail>
`
})
export class ContainerComponent {
hero = new Hero(1, 'Windstorm');
heroDeleted(hero: Hero) {
hero.name = 'Ex-' + hero.name;
}
}
複製代碼
結構指令:css
<li *ngFor="let hero of heroes">
{{ hero }}
</li>
複製代碼
<li *ngIf="isHidden">
{{ hero }}
</li>
複製代碼
屬性指令:html
# 動態綁定,更新會觸發對應子組件
<app-hero-detail [hero]="currentHero"></app-hero-detail>
# 綁定字符串,非變量值
<app-item-detail childItem="parentItem"></app-item-detail>
複製代碼
<w-button (click)="handlerClick" />
# 雙向數據綁定
<input [(ngModel)]="currentItem.name">
# 等效於
<input [value]="currentItem.name"
(input)="currentItem.name=$event.target.value" >
複製代碼
<button [attr.aria-label]="help">help</button>
<div [class.special]="isSpecial">Special</div>
<button [style.color]="isSpecial ? 'red' : 'green'">
<button [class]="{foo: true, bar: false}" />
複製代碼
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-sizer',
templateUrl: './sizer.component.html',
styleUrls: ['./sizer.component.css']
})
export class SizerComponent {
@Input() size: number | string;
@Output() sizeChange = new EventEmitter<number>();
dec() { this.resize(-1); }
inc() { this.resize(+1); }
resize(delta: number) {
this.size = Math.min(40, Math.max(8, +this.size + delta));
this.sizeChange.emit(this.size);
}
}
# html
<app-sizer [(size)]="fontSizePx"></app-sizer> 等效於
<app-sizer [size]="fontSizePx" (sizeChange)="fontSizePx=$event"></app-sizer>
複製代碼
<div *ngIf="isLoggedIn">
Welcome back, friend.
</div>
# 等效於
<ng-template [ngIf]="isLoggedIn">
Please friend, login.
</ng-template>
複製代碼
# html
<fa-input icon="envelope">
<i class="fa fa-envelope"></i>
<input inputRef type="email" placeholder="Email">
</fa-input>
# js component
@Component({
selector: 'fa-input',
template: `
<ng-content ></ng-content> # 匹配fa-input全部其餘的沒有指定匹配的
<ng-content select="input"></ng-content> # 匹配fa-input中特定的input標籤
`})
export class FaInputComponent {
...
}
複製代碼
import { Injectable } from '@angular/core';
# inject root 利用webpack tree-shaking,優化打包
@Injectable({
providedIn: 'root',
})
export class UserService {
}
複製代碼
@NgModule({
...
providers: [UserService],
...
})
複製代碼
#html
<div #mydiv><input></div>
# js
@ViewChild('mydiv') mydiv: ElementRef
constructor(
private el:ElementRef,
private renderer2: Renderer2){
}
ngOnInit(){
this.renderer2.setStyle(this.el.nativeElement.querySelector('.btn1'),'background','green');
}
# 儘可能減小應用層與渲染層之間強耦合關係, 推薦下方的render2
複製代碼
# render2 api
abstract data: {...}
destroyNode: ((node: any) => void) | null
abstract destroy(): void
abstract createElement(name: string, namespace?: string): any
abstract createComment(value: string): any
abstract createText(value: string): any
abstract appendChild(parent: any, newChild: any): void
abstract insertBefore(parent: any, newChild: any, refChild: any): void
abstract removeChild(parent: any, oldChild: any, isHostElement?: boolean): void
abstract selectRootElement(selectorOrNode: any, preserveContent?: boolean): any
abstract parentNode(node: any): any
abstract nextSibling(node: any): any
abstract setAttribute(el: any, name: string, value: string, namespace?: string): void
abstract removeAttribute(el: any, name: string, namespace?: string): void
abstract addClass(el: any, name: string): void
abstract removeClass(el: any, name: string): void
abstract setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void
abstract removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void
abstract setProperty(el: any, name: string, value: any): void
abstract setValue(node: any, value: string): void
abstract listen(target: any, eventName: string, callback: (event: any) => boolean | void): () => void
複製代碼
@Component({
selector: 'fa-input',
template: `
<i class="fa" [ngClass]="classes"></i>
`,
styleUrls: ['./fa-input.component.css']
})
export class FaInputComponent {
@Input() icon: string;
get classes() {
const cssClasses = {
fa: true
};
cssClasses['fa-' + this.icon] = true;
return cssClasses;
}
}
複製代碼
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[highLight]' # 定義指令
})
export class HighLightDirective{
colors = [
'hotpink', 'lightskyblue', 'goldenrod', 'peachpuff'
];
@HostBinding('style.color') color: string;
@HostBinding('style.borderColor') borderColor: string; # 定義樣式
@HostListener('keydown') onKeydown(){ # 定義監聽
const color = Math.floor(Math.random() * this.colors.length);
this.color = this.borderColor = this.colors[colorPick];
}
}
複製代碼
內聯元素以及沒有 CSS 樣式的元素的 clientWidth 屬性值爲 0。Element.clientWidth 屬性表示元素的內部寬度,以像素計。該屬性包括內邊距 padding,但不包括邊框 border、外邊距 margin 和垂直滾動條(若是有的話)。 當在根元素(元素)上使用clientWidth時(或者在上,若是文檔是在quirks(怪異)模式下),將返回viewport的寬度(不包括任何滾動條).node
始終指內容寬度,不包括borderjquery
# scss 文件
input {
border: none;
outline: none;
}
# 界面運行時css
input[_ngcontent-c0] {
border: none;
outline: none;
}
# 此時須要添加編譯處理
:host ::ng-deep input {
border: none;
outline: none;
}
複製代碼