Skip to content

Instantly share code, notes, and snippets.

@tbassetto
Created October 30, 2015 08:39
Show Gist options
  • Save tbassetto/fa36518660cfcc7e0b62 to your computer and use it in GitHub Desktop.
Save tbassetto/fa36518660cfcc7e0b62 to your computer and use it in GitHub Desktop.
Angular2 + Material Design Lite
import {bootstrap, Component, Inject, ElementRef, onInit} from 'angular2/angular2';
@Component({
selector: 'my-app',
template: `
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">Title</span>
<!-- Add spacer, to align navigation to the right -->
<div class="mdl-layout-spacer"></div>
<!-- Navigation. We hide it in small screens. -->
<nav class="mdl-navigation mdl-layout--large-screen-only">
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
</nav>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">Title</span>
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
</nav>
</div>
<main class="mdl-layout__content">
<div class="page-content"><!-- Your content goes here --></div>
</main>
</div>
`
})
class AppComponent implements onInit {
constructor(@Inject(ElementRef) elementRef: ElementRef) {
this.elementRef = elementRef;
}
onInit() {
// window.componentHandler.upgradeElement is provided by Material Design Lite
// and is necessary to call in order to "augment" dynamically added HTML
window.componentHandler.upgradeElement(this.elementRef.nativeElement.firstElementChild);
}
}
bootstrap(AppComponent);
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Material Design Lite</title>
<meta name="description" content="GDG Oslo DevFest 2015">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.45/angular2.dev.js"></script>
<!-- <script src="https://code.angularjs.org/2.0.0-alpha.45/router.dev.js"></script> -->
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.5/material.min.js"></script>
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.5/material.indigo-pink.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true }
});
System.import('./app.ts');
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
{
"name": "angular2-mdl",
"version": "1.0.0",
"description": "AngularJS 2 + Material Design Lite",
"scripts": {
"start": "live-server --open=."
},
"author": "Thomas Bassetto",
"license": "MIT",
"devDependencies": {
"live-server": "^0.8.2"
}
}
@SwyserDev
Copy link

SwyserDev commented Jul 6, 2016

I used:
declare var componentHandler: any;

And then the following class function in my root component (app.ts):
ngAfterViewInit() { componentHandler.upgradeAllRegistered(); console.log('MDL components upgraded...'); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment