I am using Angular 2.4.1, and my final Hello World is the next:
My index is composed by nav bar (which has display:none) and a router-outlet for presenting different views. My default view is a carousel with a link to a specific view. When I click in this link, the specific view will be loaded and (here is the problem), I would like to show the nav-bar (display:block) as well. I am stuck, and I can´t figure out how to achieve it.
My problem is being how can I reach the nav-bar (id, or even class) in my first component, from my second component.
My best approach is changing the css property of this nav-bar in the OnInit of its component. Not working... document is not recognized.
export class SociosComponent implements OnInit {
ngOnInit(): void {
this.document.getElementById('nav-principal').style.display = "block";
}
}
Here are part of the code:
app.component
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ul id="nav-principal" style="display:none; background-color:transparent;" class="nav nav-tabs">
<li role="presentation" class="active"><a routerLink="/index">Index</a><li>
<li role="presentation"><a routerLink="/socios">Socios</a><li>
</ul>
<router-outlet></router-outlet>
`
})
export class AppComponent {
}
app.rounting
// Importar componentes y módulos para el routing
import { Routes, RouterModule } from '@angular/router';
// Componentes
import { SociosComponent } from './socios.component';
import { Index } from './index';
// Configuración de las rutas
const appRoutes: Routes = [
{ path: '', redirectTo: 'index', pathMatch: 'full' },
{ path: 'index', component: Index },
{ path: 'socios', component: SociosComponent },
];
export const routing = RouterModule.forRoot(appRoutes);
socios.component
import { Component } from '@angular/core';
@Component({
selector: 'socios',
templateUrl: '../socios.html',
})
export class SociosComponent implements OnInit {
ngOnInit(): void {
this.document.getElementById('nav-principal').style.display = "block";
}
}
Thanks guys.