Top Banner
39

AngularJS 101

Jan 15, 2015

Download

Software

AngularJS 101 by Teeraphong Chaichalermpreecha
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AngularJS 101
Page 2: AngularJS 101

Why AngularJS?

AngularJS lets you extend HTML for declaring dynamic views in web-applications.

AngularJS is a toolset for building the framework most suited to

your application development and fully extensible and works well with other libraries.

Page 3: AngularJS 101

Core Concepts

• Client-Side Templates

• Model View Controller (MVC)

• Data Binding

• Dependency Injection

• Directives

Page 4: AngularJS 101

Client-side Templates

Page 5: AngularJS 101

Model View Controller (MVC)

The core idea behind MVC is that you have clear separation in your code between

managing its data (model),

the application logic (controller),

and presenting the data to the user (view).

Page 6: AngularJS 101

Data Binding

Page 7: AngularJS 101

Dependency Injection

For example, the $scope object that does our data binding is passed to us automatically; we didn’t have to create it by calling any function.

$scope isn’t the only thing we can ask for. If we want to data bind to the location URL in the user’s browser, we can ask for an object that manages this by putting $location in our constructor.

Page 8: AngularJS 101

Directives

A powerful DOM transformation engine that lets you extend HTML’s syntax.

Examples include the double-curly notation for data binding,

ng-controller for specifying which controller oversees which part of the view,

and ng-model, which binds an input to part of the model.

We call these HTML extension directives.

We can write our own to extend HTML’s template abilities

to do anything we can dream of.

Page 9: AngularJS 101

Anatomy of an AngularJS

• Invoking Angular

• Model View Controller

• Templates and Data Binding

• Organizing Dependencies with Modules

• Formatting Data with Filters

• Changing Views with Routes and $location

• Talking to Servers

• Changing the DOM with Directives

• Validating User Input

Page 10: AngularJS 101

Invoking Angular

• Loading the Script

• Declaring Angular’s Boundaries with ng-app

Page 11: AngularJS 101

Loading the Script

Page 12: AngularJS 101

Declaring Angular’s Boundaries with ng-app

Page 13: AngularJS 101

Model View Controller

• A model containing data that represents the current state of your application.

• Views that display this data.

• Controllers that manage the relationship between your model and your views.

Page 14: AngularJS 101

Model View Controller

Page 15: AngularJS 101

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()

Page 16: AngularJS 101

Displaying Text

Page 17: AngularJS 101

Form Inputs

Page 18: AngularJS 101

1. Not everyone’s browser supports JavaScript. Let everyone see all of your content and use your app without needing to execute code in the browser.

2. Some folks use browsers that work differently. Visually impaired folks who use screen-readers and some mobile phone users can’t use sites with JavaScript.

3. Javascript works differently across different platforms. IE is usually the culprit here. You need to put in different event-handling code depending on the browser.

4. These event handlers reference functions in the global namespace. It will cause you headaches when you try to integrate other libraries with functions of the same names.

5. These event handlers combine structure and behavior. This makes your code more difficult to maintain, extend, and understand.

A Few Words on Unobtrusive JavaScript

Page 19: AngularJS 101

A Few Words on Unobtrusive JavaScript

Page 20: AngularJS 101

Lists, Tables, and Other Repeated Elements

Page 21: AngularJS 101

Hiding and Showing

Page 22: AngularJS 101

CSS Classes and Styles

Page 23: AngularJS 101

Considerations for src and href Attributes

Page 24: AngularJS 101

Expressions

We can do simple math (+, -, /, *, %), make comparisons (==, !=, >, <, >=, ⇐),

perform boolean logic (&&, ||, !) and bitwise operations (\^, &, |).

We can call functions we expose on $scope in our controller

and we can reference arrays and object notation ([ ], { }, .).

Page 25: AngularJS 101

Separating UI Responsibilities with Controllers

The $scope passed to a nested controller prototypically inherits from its parent controller’s $scope.

In this case, this means that the $scope passed to ChildController will have access to all the properties of the $scope passed to ParentController.

Page 26: AngularJS 101

Publishing Model Data with Scopes

Page 27: AngularJS 101

Observing Model Changes with $watch

Page 28: AngularJS 101

Performance Considerations in watch()

Page 29: AngularJS 101

Organizing Dependencies with Modules

Enter modules. They provide a way to group dependencies for a functional area within

your application, and a mechanism to automatically resolve dependencies (also

known as dependency injection).

Generically, we call these dependencies services, as they provide specific services to

our application.

Page 30: AngularJS 101

Organizing Dependencies with Modules

Page 31: AngularJS 101

Formatting Data with Filters

Page 32: AngularJS 101

Changing Views with Routes and $location

Page 33: AngularJS 101

Changing Views with Routes and $location

Page 34: AngularJS 101

Changing Views with Routes and $location

Page 35: AngularJS 101

Changing Views with Routes and $location

Page 36: AngularJS 101

Talking to Servers

Page 37: AngularJS 101

Changing the DOM with Directives

Page 38: AngularJS 101

Validating User Input