RWD or AWD

網站不能沒有 RWD、AWD

沒有 RWD、AWD 的網站到底會怎樣?先講結論,網站流量慢慢會掉下來,因為 Google 說「演算法以行動網站為主」,網站沒有行動網站流量就會慢慢消失,RWD 跟 AWD 都是行動網站,所以網站一定要有 RWD 或 AWD,現在用行動裝置上網的比例超過 80%,簡單來說沒有手機版的網站等於宣告死刑,沒有手機版的網站請趕快改版,即使是 B2B 網站,RWD、AWD 都不是新技術,已經是成熟的網頁設計方式,而且套版網站設計費沒有很高,趕快改版才是當務之急。
延伸閱讀:網頁設計公司不會告訴你的 5 個真相

Read more »

Decorators 是 function 掛有 @前綴符號,可以用於 classparamemtermethodproperty的前面。用來提供額外的資訊。

Read more »

Element

ng-container

ng-content

ng-template

響應式表單提供了一種模型驅動的方式來處理表單輸入,其中的值會隨時間而變化。本文會向你展示如何建立和更新基本的表單控制元件,接下來還會在一個表單組中使用多個控制元件,驗證表單的值,以及建立動態表單,也就是在執行期新增或移除控制元件。

Read more »

Commom (11)

class style bind

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Native Class and Style Attributes -->
<input class="is-danger my-button" style="border: none; color: blue" />

<!-- Angular class and style Bindings -->
<input [class.is-danger]="booleanProp" [style.border]="borderProp" />

<!-- ngClass -->
<input [ngClass]="{'is-danger': booleanProp, 'myButton': true}" />
<input [ngClass]="isDangerButton" />

<!-- ngStyle -->
<input [ngStyle]="{'border': borderProp, 'color': colorProp}" />
<input [ngStyle]="hasColorBorder" />

精通 Angular 之 NgClass 和 NgStyle

angularJs 中关于 ng-class 的三种使用方式说明

Angular - Attribute 繫結、類別繫結和樣式繫結

ngFor

1
2
3
4
5
6
7
*ngFor="
let item of items;
let idx = index;
let first = first;
let last = last;
let even = even;
let odd = odd"

使用 NgFor 時,我們可以同時搭配使用五個不同的變數,分別是:

  • index:整數值;代表目前資料在陣列中的 index
  • first:布林值;代表目前資料是否為 第一筆
  • last:布林值;代表目前資料是否為 最後一筆
  • even:布林值;代表目前資料的 index 是否為 第偶數筆
  • odd:布林值;代表目前資料的 index 是否為 第奇數筆

全端開發人員天梯

ngIf

ngIf 會直接看 DOM 不見,而 [hidden] 的這個寫法只會讓 dom 看不見

1
2
3
<div *ngIf="false">test</div>
<div [hidden]="true">
<!- [style.display]="!isLoading ? 'block' : 'none'" ->

ngIf else 的 組合技

1
2
3
4
5
<div *ngIf="display; else anotherWord">Hello</div>
<ng-template #anotherWord>
<div>World</div>
</ng-template>
<button (click)="display = !display">Toggle Display</button>

[(ngModel)]

一定要引用 import { FormsModule } from '@angular/forms'; > imports: [FormsModule]
不然會沒有辦法使用

有三種寫法都可以達到雙向綁定的效果

綁定方法1

使用 [()] 的寫法

1
2
3
<input [(ngModel)]="username">

<p>Hello {{username}}!</p>

綁定方法2

[] () 分開寫

1
2
3
<input [ngModel]="username" (ngModelChange)="username = $event">

<p>Hello {{username}}!</p>

綁定方法3

不使用 ngModel

1
2
3
<input [value]="username" (input)="username = $event.target.value">

<p>Hello {{username}}!</p>

ngMode 底層說明l

[Angular 深入淺出三十天] Day 09 - Angular 小學堂(二) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天

NgTemplateOutLet

NgPlural

NgPluraCase

1
2
3
4
5
<some-element [ngPlural]="value">
<ng-template ngPluralCase="=0">there is nothing</ng-template>
<ng-template ngPluralCase="=1">there is one</ng-template>
<ng-template ngPluralCase="few">there are a few</ng-template>
</some-element>

NgSwitch

NgSwitchCase

NgSwitchDefault

1
2
3
4
5
6
7
8
9
10
11
// homepage.component.html

<ng-container *ngFor="let link of links">
<div [ngSwitch]="link?.name">
<div *ngSwitchCase="'HOME'" style="color: blue">{{link?.name}}</div>
<div *ngSwitchCase="'ABOUT US'" style="color:red">{{link?.name}}</div>
<div *ngSwitchCase="'Students'" style="color: green">{{link?.name}}</div>
<div *ngSwitchCase="'Teachers'" style="color:violet">{{link?.name}}</div>
<div *ngSwitchDefault>output2</div>
</div>
</ng-container>

Forms (27)

Router (4)

參考 router page

angular api

upgrade/static (1)

customer directive

建立一個客制化的 directive

1
$ ng g directive [NEW_DIRECTIVE]

下列是一個監聽 input event 來改變數值寫法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
import { NgControl } from '@angular/forms';

@Directive({ selector: '[appOnlyNumber]' })

export class OnlyNumberDirective {
@Input() includeFloat: boolean = false;
@Input() floatLength: number = -1;
constructor(private el: ElementRef, private control: NgControl) {}

private run() {
let element = this.el.nativeElement as HTMLInputElement;
let re = this.includeFloat ? /[^\d.]/g : /[^\d]/g;
const overTwoDots = element.value.split('').filter((item) => item === '.').length > 1;
if (overTwoDots) {
const newValue = element.value.split('').slice(0, -1).join('');
this.control.control.setValue(newValue);
return;
}
const newValue = element.value.replace(re, '');
this.control.control.setValue(newValue);
}

@HostListener('input', ['$event'])
onInput() {
this.run();
}

ngOnDestroy(): void {
window.removeEventListener('input', this.onInput.bind(this));
}
}


Other

how to add image in assets in angular

1
2
3
<img [src]="imageSrc" [alt]="imageAlt" />

<img src="{{imageSrc}}" alt="{{imageAlt}}" />
1
2
3
4
export class sample Component implements OnInit {
imageSrc = 'assets/images/iphone.png'
imageAlt = 'iPhone'
}

angular 原生的 number 需要特別去看一下 decimaPipe

Read more »

Resolver

router 轉址後會先經過 resolver 在去到另一個 router ,因此可先在此取得資料,到新頁面時就可直接賦予值

1
ng generate resolver <folder/name> [options eg. --skip-tests]
Read more »

Angular 路由

建立 component 之後,去 /src/app/app-routing.module.ts 引進 component

Read more »