Valentine with AngularJS

43
Valentine with Angular.JS @IAmVMac

Transcript of Valentine with AngularJS

Page 1: Valentine with AngularJS

Valentine with Angular.JS

@IAmVMac

Page 2: Valentine with AngularJS
Page 3: Valentine with AngularJS

Vidyasagar Machupalli

About.MEMicrosoft MVP-Games for

WindowsDeveloper at DELLhttp://about.me/mscvidyasagar

Twitter@iAmVMac

Page 4: Valentine with AngularJS

http://angularjs.org

Page 5: Valentine with AngularJS
Page 6: Valentine with AngularJS
Page 7: Valentine with AngularJS

Transclusion

Directive

Linking

Restriction

Scope ?

Page 8: Valentine with AngularJS
Page 9: Valentine with AngularJS
Page 10: Valentine with AngularJS

MVW / MV* framework

Page 11: Valentine with AngularJS
Page 12: Valentine with AngularJS

Agenda

AngularJS FeaturesGetting StartedDirectives, Filters and Data BindingViews, Controllers and ScopeModules, Routes and Factories

Page 13: Valentine with AngularJS

Getting Started

Page 14: Valentine with AngularJS
Page 15: Valentine with AngularJS

Single Page Application (SPA)

Page 16: Valentine with AngularJS

The Challenge with SPAs

DOM Manipulation

History

Routing

Module Loading

Data Binding

Object Modeling

Ajax/Promises

Caching

View Loading

Page 17: Valentine with AngularJS
Page 18: Valentine with AngularJS

ngularJS is a full-featured SPA framework

Data Binding MVC Routing

Templates

ViewModel Views

Controllers Dependency Injection

Directives

Testing

Controllers

jqLite

Validation

FactoriesHistory

Page 19: Valentine with AngularJS

Directives, Filters and Data Binding

Page 20: Valentine with AngularJS

What are Directives?

They teach HTML new tricks!

Page 21: Valentine with AngularJS

Using Directives and Data Binding

<!DOCTYPE html>

<html ng-app>

<head>

<title></title>

</head>

<body>

<div class="container">

Name: <input type="text" ng-model="name" /> {{ name }}

</div>

<script src="Scripts/angular.js"></script>

</body>

</html>

Directive

Directive

Data Binding Expression

Page 22: Valentine with AngularJS

<html data-ng-app="">...

<div class="container" data-ng-init="names=['Dave','Napur','Heedy','Shriva']">

<h3>Looping with the ng-repeat Directive</h3> <ul> <li data-ng-repeat="name in names">{{ name }}</li> </ul> </div>

...</html>

Iterate through names

Iterating with the ng-repeat Directive

Page 23: Valentine with AngularJS

AngularJS Help for Directives

Page 24: Valentine with AngularJS

<ul> <li data-ng-repeat="cust in customers | orderBy:'name'"> {{ cust.name | uppercase }} </li></ul>

Order customers by

name property

<input type="text" data-ng-model="nameText" /><ul> <li data-ng-repeat="cust in customers | filter:nameText | orderBy:'name'"> {{ cust.name }} - {{ cust.city }}</li></ul>

Filter customers by model value

Using Filters

Page 25: Valentine with AngularJS

AngularJS Help for Filters

Page 26: Valentine with AngularJS

Demo

Page 27: Valentine with AngularJS

Views, Controllers and Scope

Page 28: Valentine with AngularJS

View Controller$scope

$scope is the "glue" (ViewModel) between a controller and a view

View, Controllers and Scope

Page 29: Valentine with AngularJS

<div class="container" data-ng-controller="SimpleController"> <h3>Adding a Simple Controller</h3> <ul> <li data-ng-repeat="cust in customers"> {{ cust.name }} - {{ cust.city }} </li> </ul></div>

<script> function SimpleController($scope) { $scope.customers = [ { name: 'Dave Jones', city: 'Phoenix' }, { name: 'Jamie Riley', city: 'Atlanta' }, { name: 'Heedy Wahlin', city: 'Chandler' }, { name: 'Thomas Winter', city: 'Seattle' } ]; }</script>

Define the controller to

use

Basic controller

$scope injected

dynamically

Access $scope

Creating a View and Controller

Page 30: Valentine with AngularJS

Demo

Page 31: Valentine with AngularJS

Modules, Routes and Factories

Page 32: Valentine with AngularJS

View Controller

*FactoryDirectives

Routes

Module

Config

$scope

Page 33: Valentine with AngularJS

ControllerFactoryDirective

Routes

Module

Config

Service

Provider

<html ng-app="moduleName">

Modules are Containers

Value

Filter

Page 34: Valentine with AngularJS

var demoApp = angular.module('demoApp', []);

What's the Array for?

var demoApp = angular.module('demoApp', ['helperModule']);

Module that demoApp depends on

Creating a Module

Page 35: Valentine with AngularJS

var demoApp = angular.module('demoApp', []);

demoApp.controller('SimpleController', function ($scope) { $scope.customers = [ { name: 'Dave Jones', city: 'Phoenix' }, { name: 'Jamie Riley', city: 'Atlanta' }, { name: 'Heedy Wahlin', city: 'Chandler' }, { name: 'Thomas Winter', city: 'Seattle' } ];

});

Define a Module

Define a Controller

Creating a Controller in a Module

Page 36: Valentine with AngularJS

The Role of RoutesSPA Demo

http://www.myspa.com

View1 View2

View4 View3

/view2

/view3

/view4

/view1

Page 37: Valentine with AngularJS

Defining Routes

var demoApp = angular.module('demoApp', ['ngRoute']);

demoApp.config(function ($routeProvider) { $routeProvider .when('/', { controller: 'SimpleController', templateUrl:'View1.html' }) .when('/view2', { controller: 'SimpleController', templateUrl:'View2.html' }) .otherwise({ redirectTo: '/' });});

Define Module Routes

Page 38: Valentine with AngularJS

SPA Demohttp://www.myspa.com

Where do Views Go in a Page?

Dynamically loaded views are injected into the shell page as a module loads:

<div ng-view></div>

<ng-view></ng-view>

ORView1

Page 39: Valentine with AngularJS

The Role of Factories

var demoApp = angular.module('demoApp', [])

.factory('simpleFactory', function () {

var factory = {};

var customers = [ ... ];

factory.getCustomers = function () {

return customers;

};

return factory;

})

.controller('SimpleController', function ($scope, simpleFactory) {

$scope.customers = simpleFactory.getCustomers();

});

Factory injected into controller at runtime

Page 40: Valentine with AngularJS

View Controller

*FactoryDirectives

Routes

Module

Config

$scope

Page 41: Valentine with AngularJS

Summary

AngularJS provides a robust "SPA" framework for building robust client-centric applications

Key features: Directives and filters Two-way data binding Views, Controllers, Scope Modules and Routes

Page 43: Valentine with AngularJS

@iAmVMac