Responsive Web Design With AngularJS Sample Chapter
Responsive Web Design With AngularJS Sample Chapter
$ 29.99 US
18.99 UK
P U B L I S H I N G
E x p e r i e n c e
D i s t i l l e d
Sa
m
pl
C o m m u n i t y
ee
Web server: A simple HTTP server such as WAMP stack can be used to host
the project.
In the previous screenshot each directory has a specific type of files to be used by the
application. The details of these directories are as follows:
assets: This directory contains sass, css, font, lib, and script
subdirectories and Ruby configuration files for SASS-based COMPASS
watcher. The details of these directories are as follows:
css: This subdirectory is the target for all the CSS files generated by
the SCSS file compilation. It contains the desktop.css, mobile.css,
and tablet.css files generated from their respective SASS files.
[ 26 ]
Chapter 2
modules. This book has used AngularJS 1.3 version to demonstrate the coded
examples. The AngularJS library file can be downloaded from https://
angularjs.org.
We can also use the AngularJS library from Google's CDN. For more
information about Google's CDN refer to the following link: http://
angularjs.blogspot.in/2012/07/angularjs-now-hostedon-google-cdn.html.
data: This directory contains all the related JSON data that we will use
in our application development. It contains personalDetail.json and
professionalDetail.json files containing profile-related data. Content
of these files are as follows:
key-value pairs. The following code shows the content of this file:
{
"name":"Sandeep Kumar Patel",
"spouse":"Surabhi Patel",
"father":"Dilip Kumar Patel",
"mother":"Sanjukta Patel",
"bloodGroup":"O+ve",
"height":"5.7 feet",
"weight":"68 kg",
"chest":"40 inch",
"address":"House no 8 Marathalli bangalore 69"
}
[ 27 ]
Chapter 2
"mobile":"+91-8105469950",
"address":"Whitefield Bangalore 560087"
}
}
that has all the HTML code for desktop type devices
The following screenshot shows the full project structure including subdirectories
and code files:
[ 29 ]
To author CSS style sheets we have used a SASS-based COMPASS file watcher to
convert SCSS files to CSS files. To install COMPASS, we need to have Ruby installed
in our system. A configuration file named config.rb needs to be added inside
the assets folder before starting the COMPASS watcher. This configuration file
provides the source and target of the SASS files from where COMPASS watcher will
read the SCSS code and convert them into CSS files. The content of the config.rb
file is as follows:
require 'compass/import-once/activate'
#root of your project when deployed:
http_path = "/"
css_dir = "css"
sass_dir = "sass"
After a successful installation, COMPASS watcher can be used to monitor the assets
directory. The watcher looks for any changes in SCSS files and moves the changes
to their respective files inside the CSS directory. The following screenshot shows the
command issued to start the COMPASS watcher:
[ 30 ]
Chapter 2
The previous diagram shows the SPA architecture for our demo profile application.
Router is the main module that chooses the device type using deviceTypeProvider.
Once the device type is identified, Router chooses the HTML template file and gives
control to ProfileController, which loads the JSON data. ProfileController
links these JSON data with the HTML template and renders it in the browser.
In the previous code, ProfileServices has two methods to access the profile data.
The details of these methods are as follows:
JSON format.
While the application is loading, these two services get called by the controller.
The following screenshot shows the firebug console loading these resources
through an AJAX call:
Device-based routing
In the device-based routing approach, the requesting devices are identified by
the application and client is redirected to the appropriate HTML template. In
this approach, different HTML templates are maintained by the application and
served to the client. To identify a device the user agent string can be used to detect
deviceType. In this chapter, we have used the approach present at http://
detectmobilebrowsers.com. It maintains a set of user agent strings for various
mobile devices and checks the incoming request against these strings through
JavaScript's regular expression.
[ 32 ]
Chapter 2
AngularJS routing
AngularJS provides a routing module to cater to an incoming request for an
appropriate template and controller pair. The AngularJS routing module code can
be found in the angular-routr.min.js file and named as ngRoute package. Like
an SPA, the routing logic is present entirely in the browser's side. To create dynamic
routing, an AngularJS application module needs to perform the following steps:
1. Set up an AngularJS application.
2. Configure a routing module.
In the previous code, the AngularJS application is initialized with the module
name profileApp using the ng-app directive. This directive is the root element
of the AngularJS application and is normally used in the body or HTML root element.
A few key points about the ng-app directive are as follows:
This directive runs with priority 0, which means it runs with the
lowest priority.
[ 34 ]
Chapter 2
$routeProvider.when('/',{
templateUrl:
'view/'+deviceType+'/profileTemplate.html',
controller: 'ProfileController',
styleType:deviceType
});
}]);
In the previous code, the routing module is injected with a device type provider.
The routing module calls the getDeviceType() method to retrieve the type of device
in string format. We will have a look at the detailed implementation of this provider
in the following section. The $routeProvider module uses the when clause to route
the request to the appropriate template view. Different values of device type and
template path are listed in the following table:
Device type
Template path
Detail
Desktop
desktop/profileTemplate.html
When deviceType is
detected as desktop, the
profile template present
inside the desktop directory is
served to the client
Mobile
mobile/profileTemplate.html
When deviceType is
detected as mobile, the profile
template present inside the
mobile directory is served to
the client
Tablet
tablet/profileTemplate.html
When deviceType is
detected as tablet, the profile
template present inside the
tablet directory is served to
the client
The controller inside the routing module then sets a global scope variable named
styleType with the device type string. This styleType controller is shared in
the root scope of the application and is used in the index.html file to load the
appropriate style sheet in the CSS format. Different possible style sheets desktop.
css, mobile.css, and tablet.css can be loaded based on the device type string.
The following code shows the external style sheet pointing to different CSS files:
<link ng-if="styleType.length > 0"
ng-href='assets/css/{{styleType}}.css'
rel='stylesheet' type='text/css'>
[ 35 ]
To hide and show the personal and professional data the $scope.selected variable
is used. The $scope.getDetail() method is used to change the value of this
variable by altering its true value.
[ 36 ]
Chapter 2
[ 37 ]
Smart device: If the specified user agent string passes the JavaScript pattern
match against the list of device names, then it is considered as a smart device.
The pattern match is performed using the test() method provided by the
JavaScript regular expression object. This method returns a Boolean value.
For a successful match, it returns the isSmart variable as true. Again, the
smart device can be either a tablet or a mobile. The following list shows the
categorization among the smart devices:
Desktop: If the device is not smart, that is, the Smart flag is false, then it is a
desktop device.
Category content
[ 38 ]
Chapter 2
<div class="row">
<div class="pic">
<h1 class="name">Sandeep Kumar Patel</h1>
<img class="profile-image"
src="http://www.gravatar.com/avatar/4205261332ff1
31e971b48db31dcb528.png"
alt="profile image"/>
</div>
</div>
ng-class: This attribute is used for styling the selected button. A selected
class is attached when the selected scope property is set to true.
ng-disabled: This attribute is for the disabling and enabling button. This
attribute also takes the Boolean value from the selected scope property.
The following code shows these buttons for category selection with the attribute
directive attached to it:
<div class="row">
<div class="about">
<button class="btn" ng-model="professional"
ng-click="getDetail($event)"
ng-class="{'selected':selected}"
ng-disabled="selected">
Professional
</button>
<button class="btn" ng-model="personal"
ng-click="getDetail($event)"
ng-disabled="!selected"
ng-class="{'selected':!selected}">
Personal
</button>
</div>
</div>
[ 39 ]
Category content
This section contains the HTML markup for the personal and professional categories.
The category content is wrapped around the display class element. The following
code shows the HTML content for the professional category:
<div class="detailProfessional" ng-show="selected">
<p class="aboutme">
{{professional.aboutme}}
</p>
<div class="section">
<div class="divider">
<h4>No of Year Experience</h4>
{{professional.years}}
<h4>Job Roles</h4>
<ol>
<li ng-repeat="role in
professional.roles">{{role}}</li>
</ol>
<h4>Languages Known</h4>
<ol>
<li ng-repeat="language in
professional.languages">{{language}}</li>
</ol>
<h4> Tools</h4>
<ol>
<li ng-repeat="tool in
professional.tools">{{tool}}</li>
</ol>
</div>
<div class="divider">
<h4>Web Technologies</h4>
<ol>
<li ng-repeat="webdev in
professional.webdevelopment">{{webdev}}</li>
</ol>
</div>
</div>
</div>
The HTML content of the personal category is rendered in the browser when the
personal category button is selected by the user. The following code shows the
HTML content of the personal category:
<div class="detailPersonal" ng-show="!selected">
<div class="section">
<div class="divider">
<h4>Full Name </h4> {{personal.name}}
[ 40 ]
Chapter 2
<h4>Spouse Name </h4> {{personal.spouse}}
<h4>Father Name </h4> {{personal.father}}
<h4>Mother Name </h4> {{personal.mother}}
<h4>Blood Group </h4> {{personal.bloodGroup}}
<h4>Height </h4> {{personal.height}}
</div>
<div class="divider">
<h4>Weight </h4> {{personal.weight}}
<h4>Chest </h4> {{personal.chest}}
<h4>Address </h4> {{personal.address}}
</div>
</div>
</div>
The complete HTML code is present inside the profileTemplate.html file and can
be downloaded from the Packt Publishing support website.
We have created different SCSS files for each device. For social media icons, we have
used the icon-font.scss file that is used by all three types of devices. The code for
this icon-font.scss file is as follows:
@font-face {
font-family: 'Mono Social Icons Font';
src: url('../font/MonoSocialIconsFont-1.10.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
.symbol, a.symbol:before {
font-family: 'Mono Social Icons Font';
-webkit-text-rendering: optimizeLegibility;
[ 41 ]
The preceding social media font file is imported using the @import statement inside
the SCSS file. The SCSS file for the desktop device is created inside the desktop.scss
file. The following code shows the desktop.scss file for desktop type devices:
@import "icon-font";
body {
background: #eee;
font-family: 'Roboto', sans-serif;
font-size: 20px;
font-weight: 300;
.my-profile-container {
[ 42 ]
Chapter 2
background: white;
width: 1024px;
margin: auto;
box-shadow: 2px 2px 2px 2px lightgrey;
.row {
.pic {
height: 300px;
text-align: center;
.name {
font-weight: 100;
text-align: center;
background: #eee;
color: green;
}
.profile-image {
border-radius: 100%;
margin: 70px 0px 50px;
}
}
.about {
display: flex;
.btn {
border: 0 none;
color: green;
height: 60px;
width: 50%;
margin: 5px 0px;
font-weight: 100;
font-size: 16px;
cursor: pointer;
&:hover {
background: green;
color: #eee;
}
&.selected {
background: green;
color: #eee;
cursor: text;
}
}
}
.display {
height: 100%;
padding: 0px 30px;
[ 43 ]
Chapter 2
Category content
You can download the complete code for the mobile view from the Packt Publishing
support website.
For mobile devices the SCSS file is created under the mobile.scss file. The following
code shows the content of the mobile.scss file:
@import "icon-font";
body {
background: #eee;
font-family: 'Roboto', sans-serif;
font-size: 15px;
font-weight: 400;
overflow:hidden;
.my-profile-container {
background: white;
width: 300px;
box-shadow: 2px 2px 2px 2px lightgrey;
margin: auto;
.row {
height: 100%;
.pic {
height: 120px;
text-align: center;
.name {
font-weight: 300;
text-align: center;
background: #eee;
color: green;
}
.profile-image {
border-radius: 100%;
}
}
.about {
display: flex;
.btn {
border: 0 none;
color: green;
height: 60px;
width: 50%;
font-weight: 100;
[ 45 ]
Chapter 2
color: green;
text-decoration: none;
font-weight: 200%;
}
}
}
}
}
[ 47 ]
Chapter 2
<span class="text">{{personal.spouse}}</span>
<h4>Father Name </h4>
<span class="text">{{personal.father}}</span>
<h4>Mother Name </h4>
<span class="text">{{personal.mother}}</span>
<h4>Blood Group </h4>
<span class="text">{{personal.bloodGroup}}</
span>
<h4>Height </h4>
<span class="text">{{personal.height}}</span>
</div>
<div class="divider">
<h4>Weight </h4>
<span class="text">{{personal.weight}}</span>
<h4>Chest </h4>
<span class="text">{{personal.chest}}</span>
<h4>Address </h4>
<span class="text">{{personal.address}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
For tablet type devices, the tablet.scss file is created. The following code shows
the content of the tablet.scss file:
@import "icon-font";
body {
background: #eee;
font-family: 'Roboto', sans-serif;
font-size: 15px;
font-weight: 400;
overflow:hidden;
.my-profile-container {
background: white;
width: 768px;
box-shadow: 2px 2px 2px 2px lightgrey;
margin: auto;
.row {
height: 100%;
.pic {
text-align: center;
.name {
font-weight: 300;
text-align: center;
background: #eee;
color: green;
margin-bottom: 0px;
[ 49 ]
Chapter 2
color: green;
}
}
}
}
.footer {
background: #eee;
text-align: center;
a {
color: green;
text-decoration: none;
font-weight: 200%;
}
}
}
}
}
Verifying responsiveness
In this section, we have used the Chrome browser and the developer console to
simulate different device user agent strings for desktop, mobile, and tablet devices.
The following screenshot shows the Chrome console emulator for different devices:
[ 51 ]
You can change the model dropdown to different devices that have been emulated
such as Samsung Galaxy, Apple iPhone, and so on. The following screenshot shows
the desktop view of the profile page for general notebooks:
[ 52 ]
Chapter 2
The following screenshot shows the mobile view of the application, where the model
name is chosen as Apple iPhone 5:
[ 53 ]
The following screenshot shows the tablet view of the profile; the model name is
iPad mini device:
Summary
In this chapter, you learned how to set up an AngularJS project, a routing module,
and a device detection provider followed by building three different HTML
template versions for desktop, tablet, and mobile devices. Towards the end of the
chapter, you learned the limitations of the dynamic routing approach. In the next
chapter, you will learn about the directive-based approach for responsive web
application development.
[ 54 ]
www.PacktPub.com
Stay Connected: