Angular-materialize
This is a collection of simple angular directives for using the features in materializecss.
About angular-materialize
This plugin is useful for you if you want to use the Materialize UI framework with angular.
It includes a number of directives, which makes sure that the components in materialize.js, which are mainly initialized on document ready, are handled by Angular in the right way.
So it is not a complete rewrite of Materialize. And it therefore depends on jQuery, because Materialize depends on jQuery.
Below is a small example to get you started, and below that documentation for the directives which are included in this plugin.
Some parts of materialize, like the CSS framework, doesn't require anything extra to work in an Angular context, and they are therefore not covered here.
A list of the features in Materialize which does not yet work with angular-materialize can be found in the readme on Github.
Getting started
This plugin and all of its dependencies are hosted on CDNs, which makes setup easy.
To get a minimal example up and running, copy the below into index.html and app.js:
<html>
<head>
<title>Minimal example</title>
<!-- Materialize CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css">
<!-- JavaScript for: jQuery, angular, materialize, and angular-materialize. All of which are needed. -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-materialize/0.2.2/angular-materialize.min.js"></script>
<!-- Your app -->
<script type="text/javascript" src="app.js"></script>
</head>
<body ng-app="materializeApp" ng-controller="BodyController">
<main>
<div class="container">
<div class="row">
<div class="section col s4">
<div input-field>
<select ng-model="select.value" material-select>
<option ng-repeat="value in select.choices">{{value}}</option>
</select>
</div>
</div>
<div class="section col s8">
<h5>You selected: <b>{{select.value}}</b></h5>
</div>
</div>
</div>
</main>
</body>
</html>
var app = angular.module('materializeApp', ['ui.materialize'])
.controller('BodyController', ["$scope", function ($scope) {
$scope.select = {
value: "Option1",
choices: ["Option1", "I'm an option", "This is materialize", "No, this is Patrick."]
};
}]);
Input-field
Materialize uses the class .input-field to manage animating labels and input-fields. But that doesn't work properly when angular dynamically creates element
Instead of adding the class input-field, use the input-field directive. This way it still works when angular dynamically creates it.
Just in Materialize, if you add the length attribute, a character-counter will appear (in the below length is set to 150).
The input
or textarea
tags has to be direct children of the element with the input-field tag.
<div input-field>
<input type="text" ng-model="dummyInputs.inputFieldInput" length="150">
<label>Input label</label>
</div>
<div input-field>
<textarea ng-model="dummyInputs.textAreaInput" class="materialize-textarea"></textarea>
<label>Textarea</label>
</div>
You can also use it as an element instead of an attribute.
<input-field>
<input type="text" ng-model="dummyInputs.inputFieldInput">
<label>Input label</label>
</input-field>
<input-field>
<textarea ng-model="dummyInputs.textAreaInput" class="materialize-textarea"></textarea>
<label>Textarea</label>
</input-field>
Input-date
The below example is where there are values in all the options, but all the options are optional.
The attributes min
and max
can be any value representing a date. It should be in a format recognised by Date Constructor.
The code in the HTML
<label for="inputCreated">Input date</label>
<input input-date
type="text"
name="created"
id="inputCreated"
ng-model="currentTime"
container=""
format="dd/mm/yyyy"
months-full="{{ month }}"
months-short="{{ monthShort }}"
weekdays-full="{{ weekdaysFull }}"
weekdays-short="{{ weekdaysShort }}"
weekdays-letter="{{ weekdaysLetter }}"
disable="disable"
min="{{ minDate }}"
max="{{ maxDate }}"
today="today"
first-day="1"
clear="clear"
close="close"
select-years="15"
on-start="onStart()"
on-render="onRender()"
on-open="onOpen()"
on-close="onClose()"
on-set="onSet()"
on-stop="onStop()" />
And the JavaScript in the controller
var currentTime = new Date();
$scope.currentTime = currentTime;
$scope.month = ['Januar', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$scope.monthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$scope.weekdaysFull = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
$scope.weekdaysLetter = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
$scope.disable = [false, 1, 7];
$scope.today = 'Today';
$scope.clear = 'Clear';
$scope.close = 'Close';
var days = 15;
$scope.minDate = (new Date($scope.currentTime.getTime() - ( 1000 * 60 * 60 *24 * days ))).toISOString();
$scope.maxDate = (new Date($scope.currentTime.getTime() + ( 1000 * 60 * 60 *24 * days ))).toISOString();
$scope.onStart = function () {
console.log('onStart');
};
$scope.onRender = function () {
console.log('onRender');
};
$scope.onOpen = function () {
console.log('onOpen');
};
$scope.onClose = function () {
console.log('onClose');
};
$scope.onSet = function () {
console.log('onSet');
};
$scope.onStop = function () {
console.log('onStop');
};
Input-clock
This is a wrapper made around the materialize-clockpicker made by chingyawhao
The bundled JavaScript only includes the wrapper JavaScript, and for this to work you need to include the JavaScript and CSS from materialize-clockpicker
The options for this wrapper are the same as the options for the original materialize-clockpicker.
Below is a small example, where the option twelvehour
has been set to false.
<label for="input_starttime">Time</label>
<input id="input_starttime" input-clock data-twelvehour="false" type="text">
Material select
Add the material-select as an attribute to the select where you want material-select.
Not needed if you add the class browser-default.
It doesn't support changing content within the select, add the class browser-default if you want that.
If you add the attribute watch
, then the material-select will re-initialize whenever something changes in the content within the select.
That includes if the number of list elements change.
But beware, while the implementation works, it is very heavy in computation, so tread lightly.
You selected: {{select.value1}}
<div input-field>
<select class="" ng-model="select.value1" material-select watch>
<option ng-repeat="value in select.choices">{{value}}</option>
</select>
</div>
Multiple select
You selected: {{select.value3}}
<div input-field>
<select class="" ng-model="select.value3" material-select multiple watch>
<option ng-repeat="value in select.choices">{{value}}</option>
</select>
</div>
Using class browser-default
You selected: {{select.value2}}
<div input-field>
<select class="browser-default" ng-model="select.value2">
<option ng-repeat="value in select.choices">{{value}}</option>
</select>
</div>
Range - noUiSlider
You can use the HTML5 range element without needing to use a directive or extra JavaScript.
If you want to use the noUiSlider jQuery plugin that ships with materializecss,
you'll need to include noUiSlider's JS and CSS file and use the nouislider
directive.
<div nouislider ng-model="value" min="5" max="300"></div>
See noUiSlider's docs for how to use it. Supported config options are: ngModel (required), min, max, step, connect, and tooltips.
Carousel
A simple directive, that enables the carousel. Just add the attribute carousel
, and the carousel will be initialized.
You can optionally set one of more of the attributes
duration
,
dist
,
shift
,
padding
,
full-width
,
height
,
indicators
or
no-wrap
, and the option will be passed to the slider.
Like so: <div carousel duration='500' no-wrap='true'></div>
The options correspond to the options in the Materialize documentation
<div carousel>
<a class="carousel-item" href="javascript:void(0);"><img src="images/nature1.jpg"></a>
<a class="carousel-item" href="javascript:void(0);"><img src="images/nature2.jpg"></a>
<a class="carousel-item" href="javascript:void(0);"><img src="images/nature3.jpg"></a>
<a class="carousel-item" href="javascript:void(0);"><img src="images/nature4.jpg"></a>
<a class="carousel-item" href="javascript:void(0);"><img src="images/nature5.jpg"></a>
</div>
Collapsible
The collapsible below is populated using ng-repeat, just to show that it is possible. You can also just manually put in the content.
In the below, the popout class is added, which makes a "Google inbox" effect. Just remove the popout class to get a normal accordion.
Since this is just a wrapper for materializes collapsible, all their customization can be used.
If you add the attribute watch
, then the collapsible will re-initialize whenever something changes in the content within the collapsible.
That includes if the number of list elements change.
But beware, while the implementation works, it is very heavy in computation, so tread lightly.
-
{{single.title}}
{{single.content}}
The code in the HTML
<ul class="collapsible popout" data-collapsible="accordion" watch>
<li ng-repeat="single in collapsibleElements">
<div class="collapsible-header"><i class="{{single.icon}}"></i>{{single.title}}</div>
<div class="collapsible-body"><p>{{single.content}}</p></div>
</li>
</ul>
And the JavaScript in the controller
$scope.collapsibleElements = [{
icon: 'mdi-image-filter-drama',
title: 'First',
content: 'Lorem ipsum dolor sit amet.'
},{
icon: 'mdi-maps-place',
title: 'Second',
content: 'Lorem ipsum dolor sit amet.'
},{
icon: 'mdi-social-whatshot',
title: 'Third',
content: 'Lorem ipsum dolor sit amet.'
}
];
Chips
The chips attribute makes sure the jQuery Plugin is run. Use this directive if you want a dynamic chips functionality.
The secondary-placeholder
is the text you see when there is no item.
And placeholder
is the text you see when there is more than one item.
The code in the HTML
<div>
<div chips ng-model="chips" secondary-placeholder="secondary placeholder" placeholder="primary placeholder"></div>
</div>
And the JavaScript in the controller
$scope.chips = [{
tag: 'Apple',
}, {
tag: 'Microsoft',
},{
tag: 'Google',
}];
Toasts
Materializes toast is a global function. Using this directive you can call a toast using a custom event (in the below click), and show a message determined by a variable in the scope.
The message is specified through the message attribute, and the duration is set though the duration attribute (defaults to 3000ms if not set).
<div class="row">
<div class="col s4" input-field>
<input id="message" type="text" ng-model="dummyInputs.message" ng-init="dummyInputs.message = 'I\'m a message'">
<label for="message">Message</label>
</div>
<div class="col s2" input-field>
<input id="toastDuration" type="text" ng-model="dummyInputs.duration" ng-init="dummyInputs.duration = 1000">
<label for="toastDuration">Toast duration</label>
</div>
<div class="input-field col s4">
<a class="waves-effect waves-light btn" message="{{dummyInputs.message}}" duration="{{dummyInputs.duration}}" toast='click'>Toast!</a>
</div>
</div>
Tooltip
The tooltipped attribute in the below makes sure that the jQuery plugin is run.
You can set attributes on the tag, matching the names of the options in the materializeCSS's version.
You can disable tooltip, passing false to tooltipped attribute.
<div class="col s8" ng-if="showTooltip">
<!-- data-position can be : bottom, top, left, or right -->
<!-- data-delay controls delay before tooltip shows (in milliseconds)-->
<a tooltipped class="btn col s4 offset-s4 l2 offset-l1" data-position="bottom" data-delay="50" data-tooltip="I am tooltip"> Bottom</a>
<a tooltipped class="btn col s4 offset-s4 l2 offset-l1" data-position="top" data-delay="150" data-tooltip="I am tooltip"> Top</a>
<a tooltipped class="btn col s4 offset-s4 l2 offset-l1" data-position="left" data-delay="250" data-tooltip="I am tooltip"> Left</a>
<a tooltipped class="btn col s4 offset-s4 l2 offset-l1" data-position="right" data-delay="550" data-tooltip="I am tooltip"> Right</a>
</div>
Pushpin
The pushpin
attribute allows you to pin a particular element to the top of the page.
You can use the pushpin-top
attribute to define distance in pixels from the top of the page where the element becomes fixed. The pushpin-offset
attribute can be used to offset the distance from the top. The pushpin-bottom
attribute can be used to set the distance in pixels from the top of the page where the elements stops being fixed.
<div class="sidebar" pushpin pushpin-top="0" pushpin-offset="0" pushpin-bottom="Infinity">
<ul id="sidebarMenu" class="menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
Dropdown
The dropdown attribute in the below makes sure that the jQuery plugin is run.
You can set attributes on the tag, matching the names of the options in the materializeCSS's version.
<!-- Dropdown Trigger -->
<a class='dropdown-button btn' href='javascript:void(0);' data-activates='demoDropdown' dropdown data-hover="true">
This is a dropdown
</a>
<!-- Dropdown Structure -->
<ul id='demoDropdown' class='dropdown-content'>
<li><a href="javascript:void(0);">Link 1</a></li>
<li><a href="javascript:void(0);">Link 2</a></li>
<li><a href="javascript:void(0);">Link 3</a></li>
<li><a href="javascript:void(0);">Link 4</a></li>
<li><a href="javascript:void(0);">Link 5</a></li>
</ul>
Modals
You need a modal trigger, like the below. The trigger should have the modal directive, and the href attribute must point to the ID of the modal structure.
The modal strcture should be structured just like in materialize.
In the below example, you can change the header of the modal through the input field.
<!-- Modal Trigger -->
<div class="col">
<a class='btn' data-target='demoModal' modal>Show Modal</a>
</div>
<!-- Modal Structure -->
<div id="demoModal" class="modal">
<div class="modal-content">
<h4>Modal header</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
</div>
</div>
Tabs
If you are going to use tabs in a modal you will need to add the enable-tabs
attribute to your modal trigger.
<!-- Modal Trigger -->
<div class="col">
<a class='btn' data-target='demoModal' enable-tabs="true" modal>Show Modal</a>
</div>
Parallax
Adding the parallax attribute makes sure that the jQuery plugin from Materialize is run.
It works just like the code below the image shows, and fills the entire width of the container it is put in.
Show/hide the below parallax<div class="parallax-container">
<div parallax><img src="images/parallax1.jpg"></div>
</div>
ScrollFire
It's previously stated that this library is mostly wrappers for Materialize code.
This directive is an exception, since the Materialize code for ScrollFire is crap (as of version 0.97.5).
ScrollFire is very simple, you set an attribute scoll-fire
on some attribute, with a callback. And this callback fires when the user scrolls past that element.
One can further set an offset
attribute, to make the callback fire after the user has scrolled a certain amount of pixels beyond the element.
If you look at the source of this directive, it is used to alert a message after the user has scrolled 500 pixels past the "ScrollFire" header.
And the code below shows the source-code for that header. That example requires an toast
function to be defined on the scope.
<h2 class="header" scroll-fire="toast('Past scoll-fire', 2000)" offset="500">ScrollFire</h2>
<a href="#" class="button-collapse" data-activates="nav-mobile" data-sidenav="left" data-menuwidth="500" data-closeonclick="false">
Show side-nav
</a>
Slider
An extremely simple directive, that just enables the slider. Just add the attribute slider
, and the slider will be initialized.
You can optionally set one of more of the attributes height
, transition
,
interval
or indicators
, and the option will be passed to the slider.
Like so: <div slider height='500' transition='400'></div>
The options correspond to the ones in the Materialize documentation
<div class="slider" slider>
<ul class="slides">
<li>
<img src="http://lorempixel.com/580/250/nature/1"> <!-- random image -->
<div class="caption center-align">
<h3>This is our big Tagline!</h3>
<h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
</div>
</li>
<li>
<img src="http://lorempixel.com/580/250/nature/2"> <!-- random image -->
<div class="caption left-align">
<h3>Left Aligned Caption</h3>
<h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
</div>
</li>
<li>
<img src="http://lorempixel.com/580/250/nature/3"> <!-- random image -->
<div class="caption right-align">
<h3>Right Aligned Caption</h3>
<h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
</div>
</li>
<li>
<img src="http://lorempixel.com/580/250/nature/4"> <!-- random image -->
<div class="caption center-align">
<h3>This is our big Tagline!</h3>
<h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
</div>
</li>
</ul>
</div>
Media
Creating the above image with the effect is as simple as adding a class to the image tag.
<img materialboxed class="materialboxed responsive-img" width="650" src="images/sample-1.jpg">
Captions
It is very easy to add a short caption to your photo. Just add the caption as a data-caption
attribute.
Creating the above image with the effect is as simple as adding a class to the image tag.
<img materialboxed class="materialboxed" data-caption="A picture of some deer and tons of trees" width="250" src="iamges/nature_portrait_by_pw_fotografie-d63tx0n.jpg">
Tabs
This is a very simple directive, that just calls the jQuery plugin when the element is initialized.
For the styling to work, you still need to have the class tabs
on the ul
tag
Because of the simple implementation, don't expect ng-repeat ng-model or similar to work inside the ul of the tabs (should work inside the content though).
It is possible to trigger reload of tabs manually with a simple boolean property on your scope. It can be useful when you want to load your content dynamically with ng-include
or so.
<div class="row" ng-if="showTabs">
<div class="col s12">
<ul tabs reload="allTabContentLoaded">
<li class="tab col s3"><a href="#test1">Test 1</a></li>
<li class="tab col s3"><a class="active" href="#test2">Test 2</a></li>
<li class="tab col s3"><a href="#test3">Test 3</a></li>
<li class="tab col s3"><a href="#test4">Test 4</a></li>
</ul>
</div>
<div id="test1" class="col s12">Test 1</div>
<div id="test2" class="col s12">Test 2</div>
<div id="test3" class="col s12">Test 3</div>
<div id="test4" class="col s12">Test 4</div>
</div>
Pagination
This is only the pagination buttons, it doesn't do anything to change the page. That you'll have to do in the callback
In the below, changePage is a function defined on the controller.
Pr. default the prev/next buttons are icons from Material design icons, which needs to be included, like this: <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
It is also possible to set the option use-simple-prev-next
to false, in which case simple ">" chars will be used.
<pagination
page="1"
page-size="10"
total="200"
show-prev-next="true"
use-simple-prev-next="false"
dots="...."
hide-if-empty="false"
adjacent="2"
scroll-top="false"
pagination-action="changePage(page)" />