在Angular2裡面如何做一個全域型的變數然後當其更新時,讓所有有用到的人都知道。這時候就要用EventEmitter了
[Angular] AngularFire2
Angular2也有一個給Firebase使用的Library, 必計是自家的服務,有相對的AngularFire也是正常的,
目前AngularFire2只支援Firebase SDK V2版本,V3版本還在路上。所以在firebase所建立的專案必須要由舊介面建立後再轉至新Console介面,才可以使用。
AngularFire2現在支援Firebase SDK V3版本了,設定方式如下
[Angular]What's Structural Directives
Angular2 Directives - Structural Directives : 改變DOM element的Directive。常見的structural directives有 ngIf, ngSwitch, ngFor 用法如下.
[Angular]Rxjs的Observers
[Angular]升級到RC
Angular發佈RC了,來升級吧
[Coding4Fun] Build my own Markdown editor - Part I
因為找不到自己想要的Markdown編輯器,所以自己來寫一個.
想要的功能
[Angular] css style
Angular Component 處理styles有三種模式 1. ViewEncapsulation.None: 適用於全部頁面(No Shadow DOM) 2. ViewEncapsulation.Native: 僅套用於Shadow DOM自己本身 3. ViewEncapsulation.Emulated: 預設行為。 會自動將每個component給予一個名稱,讓各compoent裡面的style會各自獨立
先來看第1,2種,看看style會被放在哪一個位置
- ViewEncapsulation.None
1 | @Component({ |
2.ViewEncapsulation.Native
1 | @Component({ |
注意到ViewEncapsulation.Native將ViewEncapsulation.None的和本身定義的Style都包含在Shadow DOM裡面,這表示該Componenet與外面已經分開了. 本身所定義的css樣式不會影響到別人了
- ViewEncapsulation.Emulated為預設行為,會自動將每個Component給予一個名稱,然後在產生html時會將各Componet裡 定義的style加上該名稱,讓css不會互相影響
1 | @Component({ |
1 | <style>h3[_ngcontent-jfs-3] { |
參考
[Angular] How about Form?
在Angular2的Form, 可以使用[(ngModel)]的方式或是使用 ngFormModel的方式(如下)。 ngModel就不多描述,用法跟Angular1.x一樣
1 | <form [ngFormModel]="form" (submit)="add($event)"> |
js的部分如下
1 | import { Component } from 'angular2/core'; |
有幾個新東西出現. ngFormModel, ngControl. 這些都包含在FORM_DIRECTIVES裡.
FormBuilder是用來組合東西給ngFormModel用, 是把Control包在一起 builder.group({ … controls}) Control是配合ngControl使用. 初始是 new Control(『default value』, validator, asyncValidator)
這樣的設定方式,可以讓Form上的動作都在javascript裡面設定。單純html的程式碼
小問題(till Verstion Beta15)
- input[type=number]空白時,預設的值會變成NaN. 但是這個被判斷是有值得,所以Validator.required是真的. 這個問題在github上有被提出來也被解決了,但是就到目前為止,修正未包含在裡面. 所以必須手動修改程式碼, 修改方式如下 修改檔案 檔案位置: angular2\src\common\forms\directives\number_value_accessor.js
1 | NumberValueAccessor.prototype.registerOnChange = function (fn) { |
- date format設定. 原本 book.date | date:"yyyy-MM-dd"這樣子的寫法輸出的結果會是 2016-04-20. 但是現在的版本locale是被寫死的(en-US), 所以也是要進程式碼手動修改. 檔案位置: angular2\src\common\pipes
1 | // var defaultLocale = 'en-US'; 修改這裡。參數可以參考moment.js網站 |
參考文章
[Angular] Make asp.net mvc core always serve index.html
Angular2的預設route模式是html5,這個在跑到其他頁面後,在重新整理頁面後會出現空白頁面,這是因為找不到Index的入口了。所以解決方式就是要讓MVC Core可以每次都載入Index.html頁面(如果純靜態頁面的方式)
在startup.cs的Configure function, 加入以下程式碼
1 | app.Use(async (context, next) => |
在Angular2的index.ts裡設定讓所有的request的header都新增 X-Custom-Header=api,讓server判斷是否為api call
1 | import { HTTP_PROVIDERS, BaseRequestOptions, RequestOptions, Headers } from 'angular2/http'; |
這樣子就可以讓Angular2跑到不同頁面時,不會因為重新整理而出現空白的畫面
如果是用Controller/View的方式,設定方法比照舊的設定方式即可
[Angular] play with Redux part 3 - middleware
Redux2的middleware是介於action和reducer之間。例如: ReduxThunk. 設定方式是在建立store時,將middleware指定給store即可
寫自訂的middleware基本架構如下
1 | import isPromise from '../utils/is-promise'; |
store的設定方式如下
1 | function configureStore(initialState) { |
參考文件: - middleware - redux-thunk