| 388 | `; |
| 389 | |
| 390 | @Component({ |
| 391 | selector: 'app-container', |
| 392 | encapsulation: ViewEncapsulation.None, |
| 393 | template: ` |
| 394 | <button (click)="toggle()">{{ state() ? 'Leave' : 'Enter' }}</button> |
| 395 | |
| 396 | <ng-template #template> |
| 397 | <ng-content></ng-content> |
| 398 | </ng-template> |
| 399 | `, |
| 400 | }) |
| 401 | class Container { |
| 402 | private readonly viewContainerRef = inject(ViewContainerRef); |
| 403 | |
| 404 | protected readonly template = viewChild.required<TemplateRef<any>>('template'); |
| 405 | |
| 406 | protected state = signal<boolean>(false); |
| 407 | |
| 408 | protected toggle() { |
| 409 | this.state() ? this.destroy() : this.create(); |
| 410 | } |
| 411 | |
| 412 | protected create() { |
| 413 | this.state.set(true); |
| 414 | this.viewContainerRef.createEmbeddedView(this.template()); |
| 415 | } |
| 416 | |
| 417 | protected destroy() { |
| 418 | this.state.set(false); |
| 419 | this.viewContainerRef.clear(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | @Component({ |
| 424 | selector: 'app-content', |