Angular form控件原生HTML代码里ng-reflect-form属性和其值的生成时机
簡單的Component代碼:
import { Component } from '@angular/core'; import { FormControl } from '@angular/forms';@Component({selector: 'app-reactive-favorite-color',template: `Favorite Color: <input type="text" [formControl]="favoriteColorControl">` }) export class ReactFormComponent {favoriteColorControl = new FormControl(''); }如下圖所示:ng-reflect-form這個屬性運(yùn)行時(shí)是如何生成的?
bootstrap里調(diào)用_loadComponent:
changeDetectorRef的策略:RootViewRef:
ComponentRef指向的AppComponent,能看到Component的property:
loadComponent里顯式調(diào)用tick:
tick函數(shù)里進(jìn)行change detect:
Angular Component html里加了方括號的Directive,瀏覽器是無法識別的,在Angular core.js里,在執(zhí)行Component template實(shí)現(xiàn)函數(shù)時(shí),會調(diào)用Angular的??property函數(shù),將中括號包裹的Angular指令展開:Update a property on a selected element.
element指向input控件:
需要添加到input標(biāo)簽頁的屬性名稱為form:
經(jīng)過normalizeDebugBindingValue里面的正則化處理完屬性form之后,返回的值:
ng-reflect-form
待寫入input ng-reflect-form屬性的值:
為了避免value的值過于復(fù)雜時(shí),序列化生成的json字符床過大,污染了最后的原生html,在normalizeDebugBindingValue里只是簡單的調(diào)用toString函數(shù):
// Limit the size of the value as otherwise the DOM just gets polluted.
這就是最后在Chrome開發(fā)者工具里看到的[object Object]的由來:
important takeaway
setNgReflectProperty函數(shù)
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的Angular form控件原生HTML代码里ng-reflect-form属性和其值的生成时机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个SAP成都研究院开发工程师 2020
- 下一篇: HTML label标签学习笔记