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
AngularJS Directives Cookbook
AngularJS Directives Cookbook

AngularJS Directives Cookbook: Extend the capabilities of AngularJS and build dynamic web applications

eBook
$31.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

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

AngularJS Directives Cookbook

Chapter 2. Building a Navbar Custom Directive

In this chapter, we will cover:

  • Building a navbar directive
  • Directory structure for common components
  • Directive's controller function
  • Using the data attribute to HTML5 compile

Introduction

In this chapter, we will see how to build a navbar directive and adapt it to an AngularJS application. We will also explore some basic functionalities of AngularJS directives. We will use a Yeoman generator to facilitate our work and illustrate how to organize your directory structure following the AngularJS best practices from the community.

It is assumed that you have an intermediate knowledge of developing applications using AngularJS and modern tools such as Yeoman, Bower, and Grunt. However, the following examples are exemplified didactically.

Tip

You can find more information about Yeoman at http://yeoman.io/.

Building a navbar directive

One of the interface components most commonly used on websites and web applications are navigation menus. Although they are very simple, they are also very useful in any type of application.

Getting ready

Let's build the basis for an AngularJS application. As we mentioned before, we use the Yeoman generator: generator-angm.

Tip

You must have Node.js, Yeoman, Bower, and Grunt already installed on your machine.

Open your Terminal window and type the following commands in the order they appear:

  1. To install the generator, use the following command:
    npm install generator-angm -g
    
  2. To create the application, use the following command:
    yo angm
    
  3. To create the modules, use the following command:
    yo angm:angm-module
    
  4. Use the name about for the module name.

    Repeat the last step to create the following modules: news, company, and navbar. We will discuss this in detail later.

  5. Now, test the boilerplate code. On your Terminal, type the following command:
    grunt dev
    

All the code will be...

Directory structure for common components

A very useful way to develop applications with AngularJS is to use a code generator, use a simple Boilerplate, or to create your own generator or guidelines to follow. We have a lot of good practices spreading on the Internet. However, you should find your own way too.

We often need to write enough code to have a fast start, and putting a simple application in production requires many lines of code, so using tools like generators can be very productive. However, let's see some tips on how to organize our code in order to have a highly scalable application.

Getting ready

Regardless of a code generator or boilerplate, you should choose to organize your content in a modular way by grouping by features. Grouping files by features facilitates maintenance and allows you to scale the application more easily.

We will use the same code base for the previous recipe to explain the benefits of this kind of organization. The following screenshot shows a directory...

Directive's controller function

In this chapter, we have seen a different way to declare a directive to manipulate the DOM. In the previous chapter, we explored different ways of working with the directive's properties. In this example, we will declare an external controller to the directive without writing any code within the directive.

Getting ready

Using external controllers is very common in applications that have many customized directives. They help us separate the content in a more practical way, in the same behavior of the directives template. With separated controllers, we can use many AngularJS resources, such as services and other features.

This example serves to illustrate the flexibility that directives have.

We will use the same code as the previous example, but this time, we will place the controller's code in a separate file.

How to do it…

  1. Replace the controller's code with the following highlighted code:
    'use strict';
    
    /**
     * @ngdoc function...

Using the data attribute to HTML5 compile

The data attribute is an important and powerful feature of HTML5. It helps us to have more control over the HTML of our application, and this is not different on AngularJS applications.

Imagine that you are working on an AngularJS application and need to validate your HTML with a validating tool. Many companies require that you ensure your HTML code is valid, by quality questions or web standards, and others simply use it as a selling point for future customers.

If you try to validate an AngularJS application, it will have some or many errors related to the ng- tags. To avoid this, we must use the following solution.

Getting ready

Let's use the same navbar HTML code from the previous example to show the use of the data attribute.

How to do it…

  1. Just apply the data- attribute before all the ng- tags:
    <div class="navbar navbar-default">
      <div class="container">	
        <ul class="nav navbar-nav">
    ...
Left arrow icon Right arrow icon

Key benefits

  • • Learn how to extend HTML templates in new ways to build even better web applications with exceptional interface components
  • • Build reusable directives for large-scale AngularJS applications
  • • Create even sophisticated and impressive modern web apps with ease

Description

AngularJS directives are at the center of what makes it such an exciting – and important - web development framework. With directives, you can take greater control over HTML elements on your web pages – they ‘direct’ Angular’s HTML compiler to behave in the way you want it to. It makes building modern web applications a much more expressive experience, and allows you to focus more closely on improving the way that user interaction impacts the DOM and the way your app manages data. If you’re already using Angular, you probably recognize the power of directives to transform the way you understand and build your projects – but customizing and creating your own directives to harness AngularJS to its full potential can be more challenging. This cookbook shows you how to do just that – it’s a valuable resource that demonstrates how to use directives at every stage in the workflow. Packed with an extensive range of solutions and tips that AngularJS developers shouldn’t do without, you’ll find out how to make the most of directives. You’ll find recipes demonstrating how to build a number of different user interface components with directives, so you can take complete control over how users interact with your application. You’ll also learn how directives can simplify the way you work by creating reusable directives – by customizing them with Yeoman you can be confident that you’re application has the robust architecture that forms the bedrock of the best user experiences. You’ll also find recipes that will help you learn how to unit test directives, so you can be confident in the reliability and performance of your application. Whether you’re looking for guidance to dive deeper into AngularJS directives, or you want a reliable resource, relevant to today’s web development challenges, AngularJS Directives Cookbook delivers everything you need in an easily accessible way.

Who is this book for?

This book is for developers with AngularJS experience who want to extend their knowledge to create or customize directives in any type of AngularJS application. Some experience of modern tools such as Yeoman and Bower would be helpful, but is not a requirement.

What you will learn

  • • Build and customize external HTML templates, and create simple, effective directives for common interface components
  • • Learn how to use Controller function and any Bootstrap UI directives to manipulate the DOM and how to transform any UI library into AngularJS directives
  • • Construct an AngularJS application to use shared components and validate your HTML5
  • • Discover how to use jQuery events and manipulate the DOM using jQuery UI inside AngularJS applications
  • • Create custom directives for ongoing projects using Yeoman generators, and find out how to implement standalone directives
  • • Build reusable directives for Large AngularJS applications and extend directives to use dynamic templates
  • • Write unit test for directives using the Karma runner and Jasmine's behavior-driven development framework
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 30, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395896
Vendor :
Google
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Nov 30, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395896
Vendor :
Google
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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
$279.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 $ 76.98
Angular 2 Components
$32.99
AngularJS Directives Cookbook
$43.99
Total $ 76.98 Stars icon

Table of Contents

10 Chapters
1. Dealing with Modal and Tabs Directives Chevron down icon Chevron up icon
2. Building a Navbar Custom Directive Chevron down icon Chevron up icon
3. Customizing and Using Bootstrap UI Directives Chevron down icon Chevron up icon
4. Creating Interactive jQuery UI Directives Chevron down icon Chevron up icon
5. Implementing Custom Directives with Yeoman Generators Chevron down icon Chevron up icon
6. Using Directives to Develop Interface Components Chevron down icon Chevron up icon
7. Building Directives with Dynamic Templates Chevron down icon Chevron up icon
8. Creating Reusable Directives Chevron down icon Chevron up icon
9. Directive Unit Testing with Karma and Jasmine Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(5 Ratings)
5 star 80%
4 star 0%
3 star 0%
2 star 20%
1 star 0%
Perry Nally Jan 31, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Perfect resource for a very specific purpose. Directives were a difficult topic to wrap my brain around and this book definitely help unwrap it. Good reference in the form of recipes. Though they may not meet your specific need, you can easily modify most of them to fit your application.
Amazon Verified review Amazon
SuJo Jan 30, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
AngularJS is a wide spread, and sometimes it's really nice to not re-invent the wheel. The primary reason I have this book is because of that, why waste time when you have a really great resource. The recipes on the directives work well and are easy to follow along with, testing with Karma and Jasmine was very useful as I had no clue of their existence. I recommend this book if you're using AngularJS.
Amazon Verified review Amazon
ruben Jan 16, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Hello this is Ruben Oliva RamosAbout this book it is a great materia to guide the programming procedure to develop things that can use in your software resources.it is an interesting book becase has the main porposes and guide the reader to make this practices in the real life.Thanks
Amazon Verified review Amazon
Winston Jan 20, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
AngularJS is one of the most used javascript code libraries today. The author does a great job of presenting more than 30 plus recipes for the javascript programmer who wants to extend the capabilities of Angular.I throughly enjoyed learning about the tab and grid directives.
Amazon Verified review Amazon
Sudarsan Jan 09, 2018
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Almost filled the book with code. Concept wise not good
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 the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela