22
AngularJ S By Nishikant Taksande NetPay Merchant Services

AngularJs - Part 3

Embed Size (px)

Citation preview

Page 1: AngularJs - Part 3

AngularJS

ByNishikant Taksande

NetPay Merchant Services

Page 2: AngularJs - Part 3

Rewind

Basic Principles and Browser Event

$watch, $digest and $scope life cycle

$services, $filters

Module, Controllers, Directives, Scope, Filters, routes

Page 3: AngularJs - Part 3

Form Validation

<form name="form” novalidate> <label name="email">Your email</label> <input type="email" name="email" ng-model="email" placeholder="Email Address" /></form>

required ng-minlength ng-maxlength

custom validation

Page 4: AngularJs - Part 3

Control Variables in FormformName.inputFieldName.property

<div class="error” ng-show=“signup_form.name.$invalid">......

</div>

Unmodified Form - formName.inputFieldName.$pristine

Modified Form - formName.inputFieldName.$dirty

Valid Form - formName.inputFieldName.$valid

Invalid Form - formName.inputFieldName.$invalid

Errors - formName.inputfieldName.$error

Page 5: AngularJs - Part 3

Form Validation…

Custom model update triggersng-model-options="{ updateOn: 'blur' }”

ng-model-options="{ updateOn: 'mousedown blur' }”

<input type="text" ng-model="user.name" ng-model-options="{ updateOn: 'blur' }" />

<input type="text" ng-model="user.name" ng-model-options="{ debounce: 250 }" />

Page 6: AngularJs - Part 3
Page 7: AngularJs - Part 3

Dependency Injection…

How to get hold of dependencies?

Using ‘new’ operator

Referring global variable

Pass to it where needed

Page 8: AngularJs - Part 3

Dependency Injection…var myModule = angular.module('myModule', []);

myModule.factory('greeter', function($window) { return { greet: function(text) { $window.alert(text); } };});

var injector = angular.injector(['myModule', 'ng']);

var greeter = injector.get('greeter');

Page 9: AngularJs - Part 3

Dependency Injection…

fnText = fn.toString().replace(STRIP_COMMENTS, '');argDecl = fnText.match(FN_ARGS);forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg) { arg.replace(FN_ARG, function(all, underscore, name) { $inject.push(name); });});

Named Parameter

What happens if the JavaScript code was minified?

app.controller('DemoController', ['$scope', '$http', function ($scope, $http) {}

Page 10: AngularJs - Part 3

Dependency Injection…

ng-app=“myModule”myModule$provide.factory(‘obA’, ‘’, …)

$injector Instance

catch Instance

factory

Check cache If no cache

create new

$injector.get(a)

configure

Page 11: AngularJs - Part 3

DIRECTIVE ?

Page 12: AngularJs - Part 3

Directives

Markers on DOM element

ngBind, ngModel, ngClass

Matching directives: ng-model=“my_model”

Normalization

Page 13: AngularJs - Part 3

Directives… <div ng-controller="Controller"> Hello <input ng-model='name'> <hr/></div>

ngModel

Directive Types

<my-dir></my-dir>

<span my-dir="exp"></span>

<!-- directive: my-dir exp -->

<span class="my-dir"></span>

Page 14: AngularJs - Part 3

Directives… Embedded expression : <a ng-href="img/{{username}}.jpg">Hello {{username}}!</a>

$watch

<svg> <circle cx="{{cx}}"></circle></svg>

Error: Invalid value for attribute cx="{{cx}}"

<svg> <circle ng-attr-cx="{{cx}}"></circle></svg>

Page 15: AngularJs - Part 3

Directives… Creating directive – example 1

Directive of Directive can be composed.

Break view into separate HTML using templateUrl

Creating directive – example 2

templateUrl: function(elem, attr){ return 'customer-'+attr.type+'.html'; }

Page 16: AngularJs - Part 3

Directives…

A E C

'AEC'

Why should I use “A” or “E”?

Page 17: AngularJs - Part 3
Page 18: AngularJs - Part 3

Directives… Isolate scope of Directive

Directive

Controller

ViewdScope:

‘=myScope’ myScope = ‘cScope’

cScope: {}

html html html

dScope

Page 19: AngularJs - Part 3

Directives… //...scope: { dScope: ’=myScope'},//...

Creating directive – example 3

Page 20: AngularJs - Part 3

Module. Why?

• Each feature• Each reusable Component• Application level module

HTML Compiler

Compile

Link

Page 21: AngularJs - Part 3

Quick check

angular.copy angular.isArray

ngHide ngChange

angular.isDate

angular.toJson

ngShow

ngPaste

$http $location $log

$exceptionHandler

F u n c t i o n s

D i r e c t i v e s

S e r v i c e s

Page 22: AngularJs - Part 3

Angular JS