You are previewing Learning AngularJS.
O'Reilly logo
Learning AngularJS

Book Description

Get Started Fast with AngularJS Web Development

AngularJS is one of the most exciting and innovative new technologies emerging in the world of web development. Designed to simplify the development and testing of web applications, it also provides structure for the entire development process.

Websites are no longer simple static content—instead, websites have become much more dynamic, with a single page often serving as the entire site or application. And AngularJS allows web developers to build the necessary programming logic for such applications directly into a web page, binding the data model for the client web application to backend services and databases. AngularJS also allows the extension of HTML so that the UI design logic can be expressed easily in an HTML template file.

Learning AngularJS shows you how to create powerful, interactive web applications that have a well-structured, reusable code base that will be easy to maintain. You’ll also learn how to leverage AngularJS’s innovative MVC approach to implement well-designed and well-structured web pages and web applications.

AngularJS helps you to become a better web developer by forcing you to adhere to underlying structure and design rules.


  • Understand how AngularJS is organized and learn best practices for designing AngularJS applications

  • Find out how to define modules and utilize dependency injection

  • Quickly build AngularJS templates with built-in directives that enhance the user experience

  • Bind UI elements to your data model, so changes to your model and UI occur automatically in tandem

  • Define custom AngularJS directives that extend HTML

  • Implement zoomable images, expandable lists, and other rich UI components

  • Implement client-side services that interact with web servers

  • Build dynamic browser views to provide even richer user interaction

  • Create custom services you can easily reuse

  • Design unit and end-to-end tests for AngularJS applications

  • Register your book at for convenient access to updates, downloads, and corrections as they become available.

    Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Contents at a Glance
    5. Table of Contents
    6. About the Author
    7. Dedication
    8. Acknowledgments
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
      1. Who Should Read This Book
      2. Why You Should Read This Book
      3. What You Will Learn from This Book
      4. What Is AngularJS?
      5. How Is This Book Organized?
      6. Getting the Code Examples
      7. Finally
    12. 1. Jumping Into JavaScript
      1. Setting Up a JavaScript Development Environment Using Node.js
        1. Setting Up Node.js
        2. Using Node.js to Run JavaScript
        3. Creating an Express Web Server Using Node.js
      2. Defining Variables
      3. Understanding JavaScript Data Types
      4. Using Operators
        1. Arithmetic Operators
        2. Assignment Operators
        3. Applying Comparison and Conditional Operators
      5. Implementing Looping
        1. while Loops
        2. do/while Loops
        3. for Loops
        4. for/in Loops
        5. Interrupting Loops
      6. Creating Functions
        1. Defining Functions
        2. Passing Variables to Functions
        3. Returning Values from Functions
        4. Using Anonymous Functions
      7. Understanding Variable Scope
      8. Using JavaScript Objects
        1. Using Object Syntax
        2. Creating Custom Defined Objects
        3. Using a Prototyping Object Pattern
      9. Manipulating Strings
        1. Combining Strings
        2. Searching a String for a Substring
        3. Replacing a Word in a String
        4. Splitting a String into an Array
      10. Working with Arrays
        1. Combining Arrays
        2. Iterating Through Arrays
        3. Converting an Array into a String
        4. Checking Whether an Array Contains an Item
        5. Adding Items to and Removing Items from Arrays
      11. Adding Error Handling
        1. try/catch Blocks
        2. Throwing Your Own Errors
        3. Using Finally
      12. Summary
    13. 2. Getting Started with AngularJS
      1. Why AngularJS?
      2. Understanding AngularJS
        1. Modules
        2. Scopes and the Data Model
        3. Views with Templates and Directives
        4. Expressions
        5. Controllers
        6. Data Binding
        7. Services
        8. Dependency Injection
        9. Compiler
      3. An Overview of the AngularJS Life Cycle
        1. The Bootstrap Phase
        2. The Compilation Phase
        3. The Runtime Data Binding Phase
      4. Separation of Responsibilities
      5. Integrating AngularJS with Existing JavaScript and jQuery
      6. Adding AngularJS to Your Environment
      7. Bootstrapping AngularJS in an HTML Document
      8. Using the Global APIs
      9. Creating a Basic AngularJS Application
        1. Loading the AngularJS Library and Your Main Module
        2. Defining the AngularJS Application Root Element
        3. Adding a Controller to the Template
        4. Implementing the Scope Model
      10. Using jQuery or jQuery Lite in AngularJS Applications
        1. What Is jQuery Lite?
        2. Accessing jQuery or jQuery Lite Directly
        3. Accessing jQuery or jQuery Lite Directly
      11. Summary
    14. 3. Understanding AngularJS Application Dynamics
      1. Looking at Modules and Dependency Injection
        1. Understanding Modules
        2. Dependency Injection
      2. Defining an AngularJS Module Object
      3. Creating Providers in AngularJS Modules
        1. Specialized AngularJS Object Providers
        2. Service Providers
      4. Implementing Providers and Dependency Injection
        1. Injecting a Built-in Provider into a Controller
        2. Implementing a Custom Provider and Injecting It into a Controller
      5. Applying Configuration and Run Blocks to Modules
        1. Adding Configuration Blocks
        2. Adding Run Blocks
        3. Implementing Configuration and Run Blocks
      6. Summary
    15. 4. Implementing the Scope as a Data Model
      1. Understanding Scopes
        1. The Relationship Between the Root Scope and Applications
        2. The Relationship Between Scopes and Controllers
        3. The Relationship Between Scopes and Templates
        4. The Relationship Between Scope and Back-End Server Data
        5. The Scope Life Cycle
      2. Implementing Scope Hierarchy
      3. Summary
    16. 5. Using AngularJS Templates to Create Views
      1. Understanding Templates
      2. Using Expressions
        1. Using Basic Expressions
        2. Interacting with the Scope in Expressions
        3. Using JavaScript in AngularJS Expressions
      3. Using Filters
        1. Using Built-in Filters
        2. Using Filters to Implement Ordering and Filtering
      4. Creating Custom Filters
      5. Summary
    17. 6. Implementing Directives in AngularJS Views
      1. Understanding Directives
      2. Using Built-in Directives
        1. Directives That Support AngularJS Functionality
        2. Directives That Extend Form Elements
        3. Directives That Bind the Model to Page Elements
        4. Directives That Bind Page Events to Controllers
      3. Summary
    18. 7. Creating Your Own Custom Directives to Extend HTML
      1. Understanding Custom Directive Definitions
        1. Defining the Directive View Template
        2. Restricting Directive Behavior
        3. Adding a Controller to a Directive
        4. Configuring the Directive Scope
        5. Transcluding Elements
        6. Manipulating the DOM with a Link Function
        7. Manipulating the DOM with a Compile Function
      2. Implementing Custom Directives
        1. Manipulating the DOM in Custom Directives
        2. Implementing Event Handlers in a Custom Directive
        3. Implementing Nested Directives
      3. Summary
    19. 8. Using Events to Interact with Data in the Model
      1. Browser Events
      2. User Interaction Events
      3. Adding $watches to Track Scope Change Events
        1. Using $watch to Track a Scope Variable
        2. Using $watchGroup to Track Multiple Scope Variables
        3. Using $watchCollection to Track Changes to Properties of an Object in the Scope
        4. Implementing Watches in a Controller
      4. Emitting and Broadcasting Custom Events
        1. Emitting a Custom Event to the Parent Scope Hierarchy
        2. Broadcasting a Custom Event to the Child Scope Hierarchy
        3. Handling Custom Events with a Listener
        4. Implementing Custom Events in Nested Controllers
      5. Summary
    20. 9. Implementing AngularJS Services in Web Applications
      1. Understanding AngularJS Services
      2. Using the Built-in Services
        1. Sending HTTP GET and PUT Requests with the $http Service
        2. Using the $cacheFactory Service
        3. Implementing Browser Alerts Using the $window Service
        4. Interacting with Browser Cookies Using the $cookieStore Service
        5. Implementing Timers with $interval and $timeout Services
        6. Using the $animate Service
        7. Using the $location Service
      3. Using the $q Service to Provide Deferred Responses
      4. Summary
    21. 10. Creating Your Own Custom AngularJS Services
      1. Understanding Custom AngularJS Services
        1. Defining a value Service
        2. Defining a constant Service
        3. Using a Factory Provider to Build a factory Service
        4. Using an Object to Define a service Service
      2. Integrating Custom Services into Your AngularJS Applications
        1. Implementing a Simple Application That Uses All Four Types of Services
        2. Implementing Simple Time Service
        3. Implementing a Database Access Service
      3. Summary
    22. 11. Creating Rich Web Application Components the AngularJS Way
      1. Building a Tabbed View
      2. Implementing Draggable and Droppable Elements
      3. Adding a Zoom View Field to Images
      4. Implementing Expandable and Collapsible Elements
      5. Adding Star Ratings to Elements
      6. Summary
    23. Appendix A. Testing AngularJS Applications
      1. Deciding on a Testing Platform
      2. Understanding AngularJS Unit Tests
        1. Dependencies and Unit Tests
        2. Testing Controllers with User Input Bound to Scope Data
        3. Testing Filters
        4. Testing Simple Directives
        5. Testing Custom Directives That Use Transclusion
        6. Testing Directives that Use External Templates
      3. Understanding AngularJS End-to-End Testing
    24. Index
    25. Code Snippets