Angular matchMedia Service
This service is a set of bindings and helper functions for the browser matchMedia api. It allows you to check any media query right in your AngularJS application. It has one core method check
and few helper shortcuts (see examples below). Also, you can configure service rules
on config stage of your AngularJS app.
npm
Install via $ npm install --save-dev angular-matchmedia
or you can download archive on Releases
page.
After that just include dist/angular-matchmedia.js
file in your application (between main AngularJS library and your app js file).
Methods
Core method
- #### check(mediaQueryString)
Method allows you to check any media query right in your controller or service. You should pass just one parameter: any media query. Methods returns
true
orfalse
depending on media query result.
app;
Helpers
- #### isPhone
Method allows you to check if your application is viewed on mobile device (according to the following default media query rule:
(max-width: 767px)
).
app;
- #### isTablet
Method allows you to check if your application is viewed on tablet (according to the following default media query rule:
(min-width: 768px) and (max-width: 1024px)
).
app;
- #### isDesktop
Method allows you to check if your application is viewed on desktop (according to the following default media query rule:
(min-width: 1025px)
).
app;
- #### isPortrait
Method allows you to check if your application is viewed on device with portrait orientation (according to the following default media query rule:
(orientation: portrait)
).
app;
- #### isLandscape
Method allows you to check if your application is viewed on device with landscape orientation (according to the following default media query rule:
(orientation: landscape)
).
app;
- #### isRetina
Method allows you to check if your application is viewed on retina device (according to the following default media query rule:
(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)
).
app;
DEMO
You can check live demo of this service on Plunker.
Usage example
// add dependency to your applicationvar app = angular; // you can change default `rules` of the service (set your own breakpoints) in your app configapp; // inject service into your controller and you are ready to goapp;
Default rules (helper breakpoints)
rules = phone: '(max-width: 767px)' tablet: '(min-width: 768px) and (max-width: 1024px)' desktop: '(min-width: 1025px)' portrait: '(orientation: portrait)' landscape: '(orientation: landscape)' retina: '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)'
TODO
- add additional helpers
- ability to register custom helpers (not sure if it is needed)
- ability to override existing helper methods
License
MIT © Sergey Lysenko