关于angular:Angular-form控件原生HTML代码里ngreflectform属性和其值的生成时机

简略的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这个属性运行时是如何生成的?

bootstrap里调用_loadComponent:

changeDetectorRef的策略:RootViewRef:

ComponentRef指向的AppComponent,能看到Component的property:

loadComponent里显式调用tick:

tick函数里进行change detect:

Angular Component html里加了方括号的Directive,浏览器是无奈辨认的,在Angular core.js里,在执行Component template实现函数时,会调用Angular的ɵɵproperty函数,将中括号包裹的Angular指令开展:Update a property on a selected element.

element指向input控件:

须要增加到input标签页的属性名称为form:

通过normalizeDebugBindingValue外面的正则化解决完属性form之后,返回的值:

ng-reflect-form

待写入input ng-reflect-form属性的值:

为了防止value的值过于简单时,序列化生成的json字符床过大,净化了最初的原生html,在normalizeDebugBindingValue里只是简略的调用toString函数:
// Limit the size of the value as otherwise the DOM just gets polluted.

这就是最初在Chrome开发者工具里看到的[object Object]的由来:

important takeaway

setNgReflectProperty函数

更多Jerry的原创文章,尽在:”汪子熙”:

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理