| 399 | `; |
| 400 | |
| 401 | @Component({ |
| 402 | selector: 'app-container', |
| 403 | encapsulation: ViewEncapsulation.None, |
| 404 | template: ` |
| 405 | <button (click)="toggle()">{{ state() ? 'Leave' : 'Enter' }}</button> |
| 406 | |
| 407 | <ng-template #template> |
| 408 | <ng-content></ng-content> |
| 409 | </ng-template> |
| 410 | `, |
| 411 | }) |
| 412 | class Container { |
| 413 | private readonly viewContainerRef = inject(ViewContainerRef); |
| 414 | |
| 415 | protected readonly template = viewChild.required<TemplateRef<any>>('template'); |
| 416 | |
| 417 | protected state = signal<boolean>(false); |
| 418 | |
| 419 | protected toggle() { |
| 420 | this.state() ? this.destroy() : this.create(); |
| 421 | } |
| 422 | |
| 423 | protected create() { |
| 424 | this.state.set(true); |
| 425 | this.viewContainerRef.createEmbeddedView(this.template()); |
| 426 | } |
| 427 | |
| 428 | protected destroy() { |
| 429 | this.state.set(false); |
| 430 | this.viewContainerRef.clear(); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Component({ |
| 435 | selector: 'app-content', |