I have a component with another one of my own components in its template:
<div (click)="onEditStarted()">
...
<my-child *ngIf="someCondition"></my-child>
...
</div>
And in the parent I have:
@ViewChild(MyChildComponent) child: MyChildComponent;
But where I need child
, it is undefined
.
OnEditStarted() {
...
setTimeout(() => this.child.someMethod(), 0);
...
}
I also tried to negate the someCondition
and put it into the [hidden]
property instead of *ngIf
but it is still undefined
. Any ideas?
I think the problem is in The *ngIf directive. It's killing the my-child component so it couldn't reference it. You can try using ViewChildren
instead of ViewChild
or try to wrap the ngIf using a ng-template
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.