I have a home page where there is no navigation bar. Once after logging in I have a fixed side navigation bar. When I select some option from the side nav bar, I want the nav bar to remain but the content to disappear and make the content of the selected component to display. I have tried using *ngIf but the nav bar also disappears along with the content. Since I am new to angular, help would be appreciated.
admin.html:
<mat-sidenav-container class="example-container">
<mat-sidenav mode="side" opened>
<button mat-button class="menu-button" mat-icon-button [matMenuTriggerFor]="menu" aria-label="Example icon-button with a menu">
<mat-icon>library_books</mat-icon>
<span>Books</span>
<mat-icon>arrow_drop_down</mat-icon>
<mat-menu #menu="matMenu" class="my-menu">
<button mat-menu-item class="list">
<mat-icon>library_books</mat-icon>
<span>Book List</span>
</button>
<button mat-menu-item class="list" routerLink="/addBook" (click)="showPage()">
<mat-icon>add_circle</mat-icon>
<span>Add Book</span>
</button>
<button mat-menu-item class="list" routerLink="/editBook" (click)="showPage()">
<mat-icon>edit</mat-icon>
<span>Edit Book</span>
</button>
<button mat-menu-item class="list">
<mat-icon>delete</mat-icon>
<span>Delete Book</span>
</button>
</mat-menu>
</button>
<mat-divider></mat-divider>
<button mat-button class="menu-button" mat-icon-button [matMenuTriggerFor]="menu1" aria-label="Transaction icon-button with a menu">
<mat-icon> import_export</mat-icon>
<span>Transactions</span>
<mat-icon>arrow_drop_down</mat-icon>
<mat-menu #menu1="matMenu" class="my-menu1">
<button mat-menu-item class="list1">
<mat-icon>history</mat-icon>
<span>Transaction History</span>
</button>
<button mat-menu-item class="list1" routerLink="/lendBook" (click)="showPage()">
<mat-icon>book</mat-icon>
<span>Lend a Book</span>
</button>
<button mat-menu-item class="list1">
<mat-icon>edit</mat-icon>
<span>Edit Transaction</span>
</button>
<button mat-menu-item class="list1">
<mat-icon>delete</mat-icon>
<span>Delete Transaction</span>
</button>
</mat-menu>
</button>
<mat-divider></mat-divider>
<button mat-button class="menu-button" mat-icon-button [matMenuTriggerFor]="menu2" aria-label="Example icon-button with a menu">
<mat-icon>perm_identity</mat-icon>
<span>Members</span>
<mat-icon>arrow_drop_down</mat-icon>
<mat-menu #menu2="matMenu" class="my-menu2">
<button mat-menu-item class="list2">
<mat-icon>list</mat-icon>
<span>Members List</span>
</button>
<button mat-menu-item class="list2" routerLink="/addMember" (click)="showPage()">
<mat-icon>group_add</mat-icon>
<span>Add a Member</span>
</button>
<button mat-menu-item class="list2">
<mat-icon>edit</mat-icon>
<span>Edit Member</span>
</button>
</mat-menu>
</button>
<mat-divider></mat-divider>
<button mat-button class="menu-button" mat-icon-button [matMenuTriggerFor]="menu3" aria-label="Example icon-button with a menu">
<mat-icon>error</mat-icon>
<span>Fines</span>
<mat-icon>arrow_drop_down</mat-icon>
<mat-menu #menu3="matMenu" class="my-menu3">
<button mat-menu-item class="list3">
<mat-icon>list</mat-icon>
<span>Fine List</span>
</button>
<button mat-menu-item class="list3" routerLink="/addFine" (click)="showPage()">
<mat-icon>add_circle</mat-icon>
<span>Add Fine</span>
</button>
<button mat-menu-item class="list3">
<mat-icon>edit</mat-icon>
<span>Edit Fine</span>
</button>
</mat-menu>
</button>
<mat-divider></mat-divider>
<button mat-button class="menu-button">
<mat-icon>phone</mat-icon>
<span>Contact us</span>
</button>
</mat-sidenav>
<mat-sidenav-content *ngIf="show">
<div class="quote">
<p>
" The library is inhabited by spirits that come out of the pages at night "
</p>
<p>
-K
</p>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
admin.component.ts:
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.css']
})
export class AdminComponent implements OnInit {
constructor() { }
show: boolean = true;
ngOnInit(): void {
}
showPage() {
this.show = false;
}
}
The best approach would be to leverage Angular Routing and use the <router-outlet></router-outlet> directive to render any respective components when routing occurs. This is how single-page applications are intended to work.
have a read through this angular guide to understand the concept in more detail.
https://angular.io/guide/router-tutorial#using-angular-routes-in-a-single-page-application
Once you have read that and understand router, break your "pages" into separate components.
AddFineComponent, LendBookComponent, etc. and route to them into the child router-outlet
<mat-sidenav-content>
<router-outlet name="sub"></router-outlet>
</mat-sidenav-content>
.routing.ts
routes: Routes = [
{path:'', redirectTo: 'login', pathMatch: 'full'},
{
path: 'login',
component: LoginComponent,
},
{ path: 'admin',
component: AdminComponent,
children: [
{path: 'lendBook',component: LendBookComponent, outlet:"sub" },
{path: 'addFine',component: AddFineComponent, outlet:"sub"}
]
}
];
and update your routerLinks
routerLink="admin/addFine"
The Problem: In an angular project I have a single component for the sidenav menu and I'm trying to put other component sided with the sidenav, but since the sidenav element has full height the other component just goes to the bottom of the page. I tried to use display flex, but still goes to the bottom of the page.
Sidenav - html
<mat-drawer-container class="container" autosize>
<mat-drawer #drawer class="p-5" mode="side">
<!-- Content for side menu -->
<!-- Title -->
<h2 class="mt-5">Menu</h2>
<!-- Menu options-->
<button mat-button class="m-2" [matMenuTriggerFor]="workexperience">Experiência
Profissional</button>
<button mat-button class="m-2" [matMenuTriggerFor]="studies">Educação</button>
<button mat-button class="m-2" [matMenuTriggerFor]="skills">Habilidades</button>
<...>
<!-- Option work experience-->
<mat-menu #workexperience="matMenu">
<button mat-menu-item>text</button>
<button mat-menu-item>text</button>
</mat-menu>
<...>
</mat-drawer>
</mat-drawer-container>
<nav class="navbar navbar-light cyan darken-3 text-white fixed-top">
<button type="button" mat-button (click)="drawer.toggle()">
<mat-icon class="mx-2">menu</mat-icon>
<p class="mr-4 d-inline">Menu</p>
</button>
<button type="button" routerLink="/home" routerLinkActive="active" mat-raised-button>
<mat-icon class="d-inline mx-2" aria-label="Example home icon">home</mat-icon>
<p class="mr-4 d-inline">Home</p>
</button>
<a class="navbar-brand d-inline mx-5" routerLink="/bio">
<img class="rounded-circle z-depth-2" alt="80x80" height="35" src="https://mdbootstrap.com/img/Photos/Avatars/img%20(31).jpg"
data-holder-rendered="true">
<p class="d-inline mx-3 text-white">text</p>
</a>
</nav>
Sidenav- css
.container {
display: inline-block;
height: 100%;
background-color: white;
}
The other component that I want to be on the rigth of the navside.
The other component html
<mat-grid-list cols="4" rowHeight="500" class="m-5 sidenav-content">
<mat-grid-tile
*ngFor="let tile of tiles"
[colspan]="tile.cols"
[rowspan]="tile.rows"
[style.background]="tile.color">
{{tile.text}}
</mat-grid-tile>
</mat-grid-list>
The other component css
.sidenav-content{
display: flex;
height: 100%;
}
I know this has been asked, but none of the solutions work.
I have following html and I can get the image displayed if I use the commented img-element
<img src="assets/utgmap.jpg">
and comment away the following div-element. However I would like to have background image for the div instead and it's not working. CSS definition is in the end of the file and there's no 404 error for the image so it can be fetched but is is not displayed.
HTML:
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #drawer class="sidenav" fixedInViewport="true"
[attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
[mode]="(isHandset$ | async) ? 'over' : 'side'"
[opened]="!(isHandset$ | async)">
<mat-toolbar>Menu</mat-toolbar>
<mat-nav-list>
<a mat-list-item href="#">Link 1</a>
<a mat-list-item href="#">Link 2</a>
<a mat-list-item href="#">Link 3</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<mat-toolbar-row>
<button mat-icon-button (click)="toggleSidenav.emit()">
<mat-icon>menu</mat-icon>
</button>
<span>webgisproto</span>
<button mat-icon-button>
<mat-icon>settings</mat-icon>
</button>
<button mat-icon-button>
<mat-icon>help_outline</mat-icon>
</button>
<button mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>exit_to_app</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item (click)="logout()">
<mat-icon>exit_to_app</mat-icon>
<span>Sign out</span>
</button>
</mat-menu>
</mat-toolbar-row>
</mat-toolbar>
<app-menu></app-menu>
<!-- THIS WORKS!!
<img src="assets/utgmap.jpg">
IF I UNCOMMENT THIS AND COMMENT OUT THE FOLLOWING DIV -->
<div class="bgimg">
<div class="testclass">
<mat-list color="primary">
<mat-list-item>
<button mat-icon-button>
<mat-icon>settings</mat-icon>
</button>
</mat-list-item>
<mat-list-item>
<button mat-icon-button>
<mat-icon>help_outline</mat-icon>
</button>
</mat-list-item>
</mat-list>
</div>
</div>
</mat-sidenav-content>
CSS:
.testclass {
float: right;
}
.bgimg {
background: url('../../assets/utgmap.jpg') no-repeat center center fixed;
}
If I change the path above there will be error.
You need to set a dimension to your div. The following code will display the image in full screen.
html, body {
height: 100%;
}
.bgimg {
display:block;
width:100%;
height:100%;
background: url('../../assets/utgmap.jpg') no-repeat center center fixed;
}
I'm using angular 5 and angular material. i have a mat-sidenav-container that consists of mat-sidenav and a mat-sidenav-content. and I have a mat-toolbar in the top of my page. I want to use routerLink in my sidenav-content but it's not working however it works in my mat-toolbar. even a button click function is not working. here are my codes below
<mat-toolbar>
<mat-toolbar-row>
<div>
<button mat-icon-button class="button" (click)="sidemenu.toggle()">
<mat-icon class="menu">menu</mat-icon>
</button>
</div>
<div mat-card-avatar class="example-header-image"></div>
</mat-toolbar-row>
</mat-toolbar>
<mat-sidenav-container class="example-container">
<mat-sidenav mode="side" opened #sidemenu>
<mat-nav-list>
<mat-list-item appAccordionLink>
<a appAccordionToggle>
<mat-icon class="list-icon">inbox</mat-icon>
<span class="link">IncomingMail</span>
<span fxFlex></span>
</a>
</mat-list-item>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<a routerLink="/">Home</a>
</mat-sidenav-content>
<router-outlet></router-outlet>
</mat-sidenav-container>
I have a mat-sidenav in my project and I want to link a text to another URL, but when I use routerLink it doesn't work but it works out of the side-nav.it even works in the toolbar. what is the problem? here are my codes:
HTML:
<mat-toolbar>
<mat-toolbar-row>
<div>
<button mat-icon-button class="button" (click)="sidemenu.toggle()">
<mat-icon class="menu">menu</mat-icon>
</button>
</div>
<div mat-card-avatar class="example-header-image"></div>
</mat-toolbar-row>
</mat-toolbar>
<mat-sidenav-container>
<mat-sidenav mode="side" opened #sidemenu>
</mat-sidenav>
<mat-sidenav-content>
<div routerLink="/">home</div>
</mat-sidenav-content>
<router-outlet></router-outlet>
</mat-sidenav-container>