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