logo资料库

用AngularJS开发下一代Web应用.pdf

第1页 / 共196页
第2页 / 共196页
第3页 / 共196页
第4页 / 共196页
第5页 / 共196页
第6页 / 共196页
第7页 / 共196页
第8页 / 共196页
资料共196页,剩余部分请下载后查看
Copyright
Table of Contents
Preface
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Chapter 1. Introduction to AngularJS
Concepts
Client-Side Templates
Model View Controller (MVC)
Data Binding
Dependency Injection
Directives
An Example: Shopping Cart
Up Next
Chapter 2. Anatomy of an AngularJS Application
Invoking Angular
Loading the Script
Declaring Angular’s Boundaries with ng-app
Model View Controller
Templates and Data Binding
Displaying Text
Form Inputs
A Few Words on Unobtrusive JavaScript
Lists, Tables, and Other Repeated Elements
Hiding and Showing
CSS Classes and Styles
Considerations for src and href Attributes
Expressions
Separating UI Responsibilities with Controllers
Publishing Model Data with Scopes
Observing Model Changes with $watch
Performance Considerations in watch()
Organizing Dependencies with Modules
How Many Modules Do I Need?
Formatting Data with Filters
Changing Views with Routes and $location
index.html
list.html
detail.html
controllers.js
Talking to Servers
Changing the DOM with Directives
index.html
controllers.js
Validating User Input
Moving On
Chapter 3. Developing in AngularJS
Project Organization
Tools
IDEs
Running Your Application
With Yeoman
Without Yeoman
Testing with AngularJS
Karma
Unit Tests
End-to-End/Integration Tests
Compilation
Other Awesome Tools
Debugging
Batarang
Yeoman: Optimizing Your Workflow
Installing Yeoman
Starting a Fresh AngularJS project
Running Your Server
Adding New Routes, Views, and Controllers
The Testing Story
Building Your Project
Integrating AngularJS with RequireJS
Chapter 4. Analyzing an AngularJS App
The Application
Relationship Between Model, Controller, and Template
The Model
Controllers, Directives, and Services, Oh My!
Services
Directives
Controllers
The Templates
The Tests
Unit Tests
Scenario Tests
Chapter 5. Communicating with Servers
Communicating Over $http
Configuring Your Request Further
Setting HTTP Headers
Caching Responses
Transformations on Requests and Responses
Unit Testing
Working with RESTful Resources
The Declaration
Custom Methods
No Callbacks! (Unless You Really Want Them)
Simplified Server-Side Operations
Unit Test the ngResource
The $q and the Promise
Response Interception
Security Considerations
JSON Vulnerability
XSRF
Chapter 6. Directives
Directives and HTML Validation
API Overview
Naming Your Directive
The Directive Definition Object
Transclusion
Compile and Link Functions
Scopes
Manipulating DOM Elements
Controllers
Moving On
Chapter 7. Other Concerns
$location
HTML5 Mode and Hashbang Mode
AngularJS Module Methods
Where’s the Main Method?
Loading and Dependencies
Convenience Methods
Communicating Between Scopes with $on, $emit, and $broadcast
Cookies
Internationalization and Localization
What Can I Do in AngularJS?
How Do I Get It All Working?
Common Gotchas
Sanitizing HTML & the Sanitize Module
Linky
Chapter 8. Cheatsheet and Recipes
Wrapping a jQuery Datepicker
ng-model
Binding select
Calling select
The Rest of the Example
The Teams List App: Filtering and Controller Communication
The Search Box
The Combo Boxes
The Check Box
The Repeater
File Upload in AngularJS
Using Socket.IO
A Simple Pagination Service
Working with Servers and Login
Conclusion
Index
About the Authors
AngularJS Brad Green and Shyam Seshadri
AngularJS by Brad Green and Shyam Seshadri Copyright © 2013 Brad Green and Shyam Seshadri. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Simon St. Laurent and Meghan Blanchette Production Editor: Melanie Yarbrough Copyeditor: Rachel Leach Proofreader: Jilly Gagnon Indexer: Judith McConville Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest April 2013: First Edition Revision History for the First Edition: 2013-04-05: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449344856 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. AngularJS, the image of a thornback cowfish, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-34485-6 LSI
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii 1. 1 Introduction to AngularJS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Concepts 1 Client-Side Templates 2 Model View Controller (MVC) 3 Data Binding 3 Dependency Injection 5 Directives 5 An Example: Shopping Cart 6 Up Next 9 2. Anatomy of an AngularJS Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Invoking Angular 11 Loading the Script 11 Declaring Angular’s Boundaries with ng-app 12 Model View Controller 12 Templates and Data Binding 14 Displaying Text 15 Form Inputs 16 A Few Words on Unobtrusive JavaScript 19 Lists, Tables, and Other Repeated Elements 21 Hiding and Showing 23 CSS Classes and Styles 24 Considerations for src and href Attributes 26 Expressions 26 Separating UI Responsibilities with Controllers 27 Publishing Model Data with Scopes 28 Observing Model Changes with $watch 29 iii
Performance Considerations in watch() 31 Organizing Dependencies with Modules 33 How Many Modules Do I Need? 36 Formatting Data with Filters 37 Changing Views with Routes and $location 38 index.html 39 list.html 39 detail.html 40 controllers.js 40 Talking to Servers 41 Changing the DOM with Directives 43 index.html 44 controllers.js 44 Validating User Input 45 Moving On 46 3. Developing in AngularJS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Project Organization 47 Tools 50 IDEs 50 Running Your Application 51 With Yeoman 51 Without Yeoman 51 Testing with AngularJS 52 Karma 52 Unit Tests 54 End-to-End/Integration Tests 55 Compilation 57 Other Awesome Tools 59 Debugging 59 Batarang 60 Yeoman: Optimizing Your Workflow 64 Installing Yeoman 65 Starting a Fresh AngularJS project 65 Running Your Server 65 Adding New Routes, Views, and Controllers 65 The Testing Story 66 Building Your Project 66 Integrating AngularJS with RequireJS 67 4. Analyzing an AngularJS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Application 77 iv | Table of Contents
Relationship Between Model, Controller, and Template 78 The Model 79 Controllers, Directives, and Services, Oh My! 80 Services 80 Directives 84 Controllers 85 The Templates 89 The Tests 95 Unit Tests 96 Scenario Tests 99 5. Communicating with Servers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Communicating Over $http 101 Configuring Your Request Further 103 Setting HTTP Headers 104 Caching Responses 105 Transformations on Requests and Responses 106 Unit Testing 107 Working with RESTful Resources 108 The Declaration 111 Custom Methods 111 No Callbacks! (Unless You Really Want Them) 112 Simplified Server-Side Operations 112 Unit Test the ngResource 112 The $q and the Promise 113 Response Interception 115 Security Considerations 115 JSON Vulnerability 116 XSRF 116 6. Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Directives and HTML Validation 119 API Overview 120 Naming Your Directive 121 The Directive Definition Object 122 Transclusion 126 Compile and Link Functions 126 Scopes 128 Manipulating DOM Elements 132 Controllers 133 Table of Contents | v
Moving On 136 7. Other Concerns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 $location 137 HTML5 Mode and Hashbang Mode 140 AngularJS Module Methods 142 Where’s the Main Method? 142 Loading and Dependencies 143 Convenience Methods 144 Communicating Between Scopes with $on, $emit, and $broadcast 146 Cookies 148 Internationalization and Localization 148 What Can I Do in AngularJS? 149 How Do I Get It All Working? 149 Common Gotchas 150 Sanitizing HTML & the Sanitize Module 150 Linky 152 8. Cheatsheet and Recipes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Wrapping a jQuery Datepicker 153 ng-model 155 Binding select 155 Calling select 155 The Rest of the Example 156 The Teams List App: Filtering and Controller Communication 157 The Search Box 161 The Combo Boxes 161 The Check Box 161 The Repeater 161 File Upload in AngularJS 162 Using Socket.IO 164 A Simple Pagination Service 167 Working with Servers and Login 171 Conclusion 174 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 vi | Table of Contents
分享到:
收藏