Skip to content

Instantly share code, notes, and snippets.

@anthanh
Last active December 10, 2017 23:08
Show Gist options
  • Save anthanh/557b5aa56e4e20295ea77cf9436ed27a to your computer and use it in GitHub Desktop.
Save anthanh/557b5aa56e4e20295ea77cf9436ed27a to your computer and use it in GitHub Desktop.
Angular 5 with Universal and TransferState: Example app with material.io
# install material dependency
npm i -S @angular/material @angular/cdk
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular Universal Example</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<app-root></app-root>
</body>
</html>
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
opened: boolean;
photos: any[];
constructor(private http: HttpClient) { }
ngOnInit() {
this.http
.get('http://jsonplaceholder.typicode.com/photos?_limit=10')
.subscribe((data: any[]) => {
this.photos = data;
});
}
}
// app.component.scss
mat-sidenav-content {
padding-top: 56px;
}
mat-toolbar {
position: fixed;
top: 0;
z-index: 1;
p {
width: 100%;
text-align: center;
}
}
mat-grid-tile {
img {
object-fit: cover;
width: 100%;
height: 100%;
}
}
<!-- app.component.html -->
<mat-sidenav-container>
<mat-sidenav #sidenav mode="over" [(opened)]="opened">
<mat-list>
<mat-list-item>Option 1</mat-list-item>
<mat-list-item>Option 2</mat-list-item>
<mat-list-item>Option 3</mat-list-item>
</mat-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar>
<mat-icon (click)="sidenav.toggle()">dehaze</mat-icon>
<p>
<span>My Application</span>
</p>
</mat-toolbar>
<mat-grid-list cols="2" rowHeight="150px">
<mat-grid-tile *ngFor="let photo of photos">
<img [src]="photo.thumbnailUrl" [alt]="photo.title">
</mat-grid-tile>
</mat-grid-list>
</mat-sidenav-content>
</mat-sidenav-container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment