Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
MEAN Cookbook
MEAN Cookbook

MEAN Cookbook: The meanest set of MEAN stack solutions around

eBook
€26.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

MEAN Cookbook

Enhancing Your User Interface

This chapter will provide recipes for common user interface enhancements in your single app web application using Angular. We will learn to use Sass to control the styling of our application as well as importing common user interface tools such as the Bootstrap & Font-Awesome to our application. We'll also cover the basics of internalizing our user interface in Angular-CLI with it's built-in i18n toolkit.

In this chapter, we will cover the following recipes :

  • Configuring Sass for Angular
  • Working with Angular component styles
  • Using Sass variables for style reusability
  • Using Sass nesting for better style specificity
  • Importing Sass partials for style organization
  • Working with Bootstrap in Sass
  • Customizing Bootstrap for Angular
  • Using Bootstrap Responsive layouts
  • Working with Bootstrap components in Angular
  • Working with Font-Awesome icons...

Introduction

Building a modern web application with Angular involves adding some sort of CSS framework to your application. CSS provides a robust and rich set of styling options to manage the look and feel of your web application. However, in many respects, vanilla CSS provides us with only the very basics to build modern, maintainable styles for our web applications. Luckily, similar to the advancements made in JavaScript compilation, there has been a revolution in CSS-based pre-compilers that add many useful enhancements and features to the CSS standard.

In this chapter we will cover common CSS configurations for Angular, including how to use the most popular CSS preprocessor, Sass. We will also take a look at a variety of ways we can use Sass that is unique to working within Angular-CLI that can streamline our frontend development productivity.

...

Configuring Sass in Angular

Angular-CLI supports all the major CSS preprocessors, including Sass, Less, and Stylus. Although each of these options has its own unique features and language syntax, Sass is, by far, the most popular CSS preprocessor used for web applications. For that reason it is what we will focus on in this book.

Sass works as as superset to CSS in a similar way that TypeScript acts as a superset of JavaScript in Angular. Our Sass styles will compile during our Angular-CLI build process, and output vanilla CSS for the browser. It will also allow us to use many useful features, such as defining variables, nesting for selectors for improved specificity, and better organizing our styles for reusable and composable styling.

Getting ready

...

Working with Angular component styles

Angular has a unique way of isolating styles on components in our application by scoping CSS selectors to dynamically generated component selectors. This approach gives us component-level style isolation that will prevent our component's styles from leaking across the rest of our application.

Let's add some component styles to our /src/app/app.component.ts and /src/app/posts/posts.component.ts components. We'll give our header some padding between it and the blog content below it. Adding negative space to our user interface like this will make our blog content easier to read. We will frame the blog content itself with a border and a silver background color.

How to do it...

...

Using Sass variables for style reusability

A very handy advantage of using Sass is to define commonly used values in our styles as variables so that they can be easily reused in our project's styling.

Imagine that our Angular application has a very simple two-color theme for its background and text. For our navigation, we simply want to reverse the same two colors of theme for the background and text. With Sass, we can define these colors once and then apply them to the correct properties for the objects we want to style, without copying and pasting hexadecimal color definitions around.

How to do it...

We can define two-color variables by defining them in our /src/styles.scss stylesheet:

$background-color: #585858;
$text...

Using Sass nesting for better style specificity

Another advantage of Sass is the ability to nest selectors to increase their styling specificity, as well as make relationships between content styles more manageable. Sass's ability to nest selectors helps enforce selector specificity through a clear visual relationship with its parent selectors. This makes creating style hierarchies in your application much more visually apparent in the stylesheet itself.

Getting ready

Imagine that we have two styles of links, one that is for our general content and another that is, specifically, for our navigation. We really want to be certain that these links are styled the same way, except for their coloring. We could write these as...

Using Sass partials for style organization

Sass has more features than just its advanced CSS syntax and language features. It also allows us to more easily modularize our styles into partials for easier reuse and configuration. The modularization aspect of Sass partials is one of the most powerful features for organizing and maintaining your app's styling.

Getting ready

Let's make the color definitions for our web application's Sass variables a separate configuration file from our other Sass files. This will make updating our branding or any other color configuration changes much easier to find and make in our application.

...

Working with Bootstrap in Sass

In this section, we will discuss how to import CSS frameworks and utilities into our application to set up a basic, responsive layout in our Angular application using Bootstrap. Bootstrap is a very popular CSS framework developed by Twitter for responsive web design that works on a wide range of devices, from desktop computers to mobile phones. The latest version of Bootstrap 4 includes many improvements, including switching from Less to Sass for its source files, a new flex-box-based grid system, and many optimizations and performance tweaks to better support mobile devices.

How to do it...

Our Angular application may be modern on the inside, but its user interface is very retro looking, without...

Customizing Bootstrap for Angular

Configuring Bootstrap will let you customize the parts of the Bootstrap framework you include in your application. Loading fewer parts of Bootstrap will reduce your file size, thus, increasing the performance of compiling your Sass stylesheets.

Getting ready

Loading all of Bootstrap is great, but what if we don't want or need all of the Bootstrap framework in our Angular application? Let's remove all the built-in JavaScript components used by Bootstrap so that we can implement our own Angular-based versions instead.

How to do it...

...

Using Bootstrap Responsive layouts

One of the best reasons to use a CSS framework such as Bootstrap is to more easily support responsive layouts in our web application. Using Bootstrap's layout classes and breakpoints, we can easily add responsive design to our Angular application.

Getting ready

Bootstrap 4 supports a modern flex-box-based responsive grid system. To use the grid system, we will need to declare a container that will contain the grid, rows for each line of elements we will lay out in the container, and columns for each item in the row:

>

We can compose any combination of column widths together that we want as long as the total within the row adds up to 12 or fewer. Otherwise our column content will...

Working with Bootstrap components in Angular

Bootstrap 4 contains many different JavaScript-dependent components. Many of these components rely on jQuery for Document Object Model (DOM) manipulation and can adversely interact with Angular's own View Encapsulation based data-binding. We will solve this issue using the Angular-friendly versions of these components provided by the ng-bootstrap project.

Getting ready

Let's add an accordion component to our web application, so we can group blog posts by their author. When the user clicks on the author's name, they will see a list of blog posts they created.

In order to build this functionality, we will need to install and save the ng-bootstrap project as a dependency...

Working with Font-Awesome icons in Angular

Unlike the preceding version of Bootstrap, Bootstrap 4 does not come with a built-in icon set. You get to choose the icon set you'd like to include for your application.

The Font-Awesome project is a very popular font-based icon set with a robust collection of high-quality icons that cover a wide range of common application functionalities, all available for free as an open source resource.

Getting ready

Let's enhance our new Bootstrap accordion component by putting a user icon next to all our author's names and a newspaper icon next to their blog post titles. Before we can get started, we will need to install font-awesome as a dependency to our project. We will do...

Internationalization with Angular i18n

Modern web applications can often support millions of users distributed across the globe. Even if the audience for your application is limited, the advantages you gain from building an application supporting multiple languages or localized date and time formatting can be very important for any international customers who use it. Luckily, Angular has an advanced toolkit for managing translations and localizing content in our application.

To translate text files in Angular, we will use the i18n attribute that comes built-in with Angular core. We will use this tool to mark strings in our application for translation and run a utility in Angular-CLI to extract them. Once we've extracted the strings, we can hand them off as a static file to be translated, and we will be able to import this file to display a localized language version of our...

Setting your language with Angular i18n

Although ahead-of-time (AOT) compilation of template strings can be useful, users may often wish to be able to manually decide the language that they would like to see, and have that served to them as a just-in-time (JIT) option. To support this, we will need to add providers to Angular to watch for a locale setting we can use to determine our user's language preference.

Let's configure our application to display whatever language the browser is defaulted to using. That way, content will automatically be displayed in any translation that matches the machine that they are using to access our application.

Getting ready

Before we can get started, to take advantage of just-in-time...

How to Localize dates in Angular

Many countries format dates and times in a different way. For example, between the US and UK, the order of months and days is often reversed in shortened dates. For example, 12/5/2017 is December 5th, 2017 (MM/DD/YYYY) in the US, while it would mean May 12th, 2017 (DD/MM/YYYY). Unlocalized date format can be potentially very confusing to your users and is an easy localization option offered by Angular's built-in Pipes helper.

How to do it...

Let's follow the steps to add date localization to our blog posts so that they will display in the format our readers would expect:

  1. We'll start by adding a simple date object to our /src/app/posts/post/post.component.ts component:
postDate...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Architect a fully functional stand-alone web application, including the web server, database, and front-end web application
  • Improve the performance and maintainability of your MEAN stack application with tips for configuration and optimization
  • Highlights MEAN Stack best practices when working with your application

Description

The MEAN Stack is a framework for web application development using JavaScript-based technologies; MongoDB, Express, Angular, and Node.js. If you want to expand your understanding of using JavaScript to produce a fully functional standalone web application, including the web server, user interface, and database, then this book can help guide you through that transition. This book begins by configuring the frontend of the MEAN stack web application using the Angular JavaScript framework. We then implement common user interface enhancements before moving on to configuring the server layer of our MEAN stack web application using Express for our backend APIs. You will learn to configure the database layer of your MEAN stack web application using MongoDB and the Mongoose framework, including modeling relationships between documents. You will explore advanced topics such as optimizing your web application using WebPack as well as the use of automated testing with the Mocha and Chai frameworks. By the end of the book, you should have acquired a level of proficiency that allows you to confidently build a full production-ready and scalable MEAN stack application.

Who is this book for?

If you are a JavaScript developer who wants to create high-performing, modern web applications with the MEAN stack, this is the book for you. Web developers familiar with some parts of the MEAN stack will find this a comprehensive guide to fleshing out the other technologies and skills they need to build all JavaScript web applications. Developers interested in transitioning from other web application stacks to an all-JavaScript environment will find a wealth of information about how to work in a MEAN stack environment. To get the most from this book, you should have a general understanding of web servers and web applications. You are expected to have a basic understanding of running JavaScript, both in a web browser and outside it, using Node.js and the NPM package manager.

What you will learn

  • • Bootstrap a new MEAN stack web application using Node.js and Express
  • • Build a single-page application (SPA) with Angular and Angular-CLI
  • • Improve browser performance by optimizing your web application resources using Webpack
  • • Model complex JSON object relationships in MongoDB using Mongoose
  • • Debug all the layers of a MEAN stack application, including working with source maps
  • • Build Restful APIs using Express.js and JSON Web Token (JWT) for user authentication
  • • Use automated testing to improve the reliability and quality of your MEAN stack application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 28, 2017
Length: 450 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288157
Vendor :
Google
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 28, 2017
Length: 450 pages
Edition : 1st
Language : English
ISBN-13 : 9781787288157
Vendor :
Google
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 120.97
MEAN Cookbook
€36.99
Mastering  Node.js
€41.99
Node Cookbook
€41.99
Total 120.97 Stars icon

Table of Contents

12 Chapters
Working with Angular 4 Chevron down icon Chevron up icon
Enhancing Your User Interface Chevron down icon Chevron up icon
Working with Data Chevron down icon Chevron up icon
Using Express Web Server Chevron down icon Chevron up icon
REST APIs and Authentication Chevron down icon Chevron up icon
Cloud Service Integrations Chevron down icon Chevron up icon
MongoDB and Mongoose Chevron down icon Chevron up icon
Relationships Chevron down icon Chevron up icon
Build Systems and Optimizations Chevron down icon Chevron up icon
Debugging Chevron down icon Chevron up icon
Automated Testing Chevron down icon Chevron up icon
Whats new in Angular 4 Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(1 Ratings)
5 star 0%
4 star 0%
3 star 100%
2 star 0%
1 star 0%
John A Feb 13, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I was hoping to just ignore the angular section as I am a react developer but it is all so wrapped around angular I struggled with all sections, a mern version of this book would be ideal
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.