How does Angular2: get elements under the control of * ngIf?

page code: < div id="d" * ngIf = "show" > < / div >
ts file: in the callback function for which the request is successful:
this.show = true;
document.getElementById ("d")
at this time it is null,. I guess because dom hasn"t been rendered yet, try to change the div, to
this.show = true;
setTimeout (() = > {
document.getElementById ("d");
}, 100);
you can get dom, at this time, but it is not clear whether this will be a problem. Is there a better way?

Mar.01,2021

you can try ngAfterChecked


because I'm going to init an echart form after I get dom. So far, three solutions have been found: 1. Is the method mentioned in the question, which is executed in the callback of setTimeout and set a time. two Use [hidden] to control. 3. Make the parts that need to be controlled into subcomponents

Menu