While researching, I came across a recommendation in the Angular Docs that suggests using a directive to access the ViewContainerRef
for creating dynamic components. Here is an example of such a directive:
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[ad-host]',
})
export class AdDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
The documentation also mentioned another approach which involves using ViewChild
with {read : ViewContainerRef}
, like so:
export class AppComponent {
@ViewChild('someHashTag', {read : ViewContainerRef}) target: ViewContainerRef;
I am curious if there are any differences or potential issues between these two approaches. Is there any reason why one should not opt for the method that requires less code and effort?