在Angular中获取所有后代NgControls

时间:2019-06-17 10:14:37

标签: angular

我想在父组件中获取所有后代NgControls。为此,我在父级组件中使用ContentChildren。

我父母的组件看起来像这样:

<validation-group>
    <input type="text" id="field-1" name="field1" formControlName="field1" />
    <input type="text" id="field-2" name="field2" formControlName="field2" />
    <address-field></address-field>
</validation-group>

address-field组件的模板如下:

<input type="text" id="line-1" name="line1" formControlName="line1" />
<input type="text" id="postcode" name="postcode" formControlName="postcode" />

validation-group组件:

export class ValidationGroupComponent {
    @ContentChildren(NgControl) ngControls: QueryList<NgControl>;

    ngAfterContentInit (): void {
        // ngControls contains 2 ng controls: field-1, and field-2.
    }
}

ngAfterContentInit方法中,ngControls字段仅包含field-1和field-2。

我需要我内部的所有ngControl。

您知道为什么我无权访问地址字段的ngcontrols吗?或者,也许您知道实现该目标的其他解决方案?

0 个答案:

没有答案
相关问题