I have a header component and a sidebar component as child component of Main component as follows.
main component
@Component({
selector: 'my-app',
template: `
<body>
<app-header></app-header>
<app-sidebar></app-sidebar>
<main class="mdl-layout__content mdl-color--grey-100">
<auth-router-outlet></auth-router-outlet>
</main>
</div>
</body>
`,
directives: [ROUTER_DIRECTIVES, SidebarComponent, HeaderComponent, MDL, AuthRouterOutlet,SimpleNotificationsComponent],
providers: [AUTH_PROVIDERS,NotificationsService]
})
export class AppComponent {}
The header component has a child component. I want to destroy this child component when the user click on any of the sidebar links. Currently the child component becomes part of the header once data is rendered and breaks the layout.
HeaderComponent
@Component({
selector: 'app-header',
directives: [AptSearchComponent],
template: `
<div class="mdl-textfield__expandable-holder">
<input class="mdl-textfield__input" type="text" id="search" (keyup.enter)="Search($event)">
</div>
<aptsearch *ngIf="apartment" [model]="apartment"></aptsearch>
`,
})
export class HeaderComponent {
public apartment: string;
constructor(private apartmentService: ApartmentService,private router: Router,private sharedService: SharedService) {
this.apartmentService=apartmentService;
this.sharedService=sharedService;
}
Search(event){
this.apartment = event.target.value;
this.router.navigate(['AptSearch']);
}
}
child component of the header component, which needs to be destroyed when user click on any links from the sidebar.
@Component({
selector: 'aptsearch',
template: `
<div *ngFor="#aptdetails of aptDetails">
<h2 class="mdl-card__title-text">{{aptdetails.aptID}}</h2>
</div>
</div>
`,
directives: [ROUTER_DIRECTIVES]
})
export class AptSearchComponent implements OnInit {
@Input() model:string;
aptDetails: any;
constructor(private apartmentService: ApartmentService, private sharedService: SharedService,private zone:NgZone) {
this.apartmentService = apartmentService;
}
ngOnInit(){}
ngOnChanges(changes: any) {
console.log('Inside the Search Component from parent -->' +this.model);
this.apartmentService.searchApt2(this.model).subscribe(res => {this.aptDetails = res});
}
}
How can this be achieved ?