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
MEAN Cookbook
MEAN Cookbook

MEAN Cookbook: The meanest set of MEAN stack solutions around

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

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781787286573
Vendor :
Google
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Sep 28, 2017
Length: 450 pages
Edition : 1st
Language : English
ISBN-13 : 9781787286573
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
Banner background image

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.