简体   繁体   English

ngForm参考变量在ng-template中返回未定义

[英]ngForm reference variable return undefined in ng-template

I am trying to inject a form in a custom component and I am dynamically loading that component. 我正在尝试在自定义组件中注入表单,并且正在动态加载该组件。 For this case, while referencing the ngForm template variable, I am getting undefined: 对于这种情况,在引用ngForm模板变量时,我无法定义:

<app-custom-component [template]="myTemplate">
    <ng-template #myTemplate>
        <form #myForm="ngForm">
            .....
        </form>
    </ng-template>
</app-custom-component>

Now in my component.ts 现在在我的component.ts

@ViewChild('myForm') myForm;
ngAfterViewInit() {
    this.buyAmountForm.valueChanges.subscribe(values => this.validate()); //--> undefined, no valueChanges
}

If I remove the ng-template , everything works fine. 如果我删除ng-template ,一切正常。 Any workaround to get past this? 有什么解决方法可以解决这个问题?

使用{descendants: true}访问后代

@ViewChild('myForm', {descendants: true}) myForm;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM