AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form...

10
HTML enhanced for web apps!

Transcript of AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form...

Page 1: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

HTML enhanced for web apps!

Page 2: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Agenda – Day 2

Exercise discussion / QA

Services and Factories

Dependency Injection (DI)

Demo - SPA Routing via ‘ngRoute’

Form Validation

Page 3: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Services Reusable business logic, independent of views

Can be used by controllers and other services/components

Defined like this –

Many flavors – factories , services & providers

Mainly differ in their creational pattern

angular.module('myModule', []).

factory('greeter', function(someDependency) {

// do some initialization here, any internal methods,

// if required

return {

myMethod: function(text) {

return text.toUpperCase();

}

};

});

Page 4: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Dependency Injections (DI)

Creates and wires objects/functions

Freedom from creating and managing services, internal dependencies

No need of doing ‘new’ for components

No more inter-dependency management

Handled by ‘Injector’ sub-system

All services, modules registered via Ids – $scope, $http, greeter etc.

Modules assist in registering their own components

Crucial in writing unit and end-to-end tests

Injector sub-system != requireJS/ AMD.js

Page 5: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

SPA Routing

Support provided in ‘ngRoute’ module

Download reference - https://docs.angularjs.org/api/ngRoute

Create route maps –

Use ‘location’ service to manually move from one view to another $location.path('/login');

myApp.config(function($routeProvider) {

$routeProvider.when('/login', {

templateUrl: 'partials/Login.html',

controller: 'LoginController'

}).when('/welcome', {

templateUrl: 'partials/Todo.html',

controller: 'ToDoController'

}).otherwise({

redirectTo: '/login'

});

});

Page 6: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Demo• Routing via ngRoute

• Services

• Hands-on: Controllers & Services

• with broadcast and emit events

Page 7: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Form Validation Start by - adding ‘name’ attribute to ‘form’ and ‘form elements’

Make use of these validation directives -

required, type, ng-minlength, ng-maxlength, ng-pattern

Your own custom directive

Angular attaches these properties to form and form elements

Accessed as

Form: <form name>.<property>

Individual form element: <form name>.<element name>.<property>

Applies these styling classes –

.ng-valid, .ng-invalid, .ng-pristine, .ng-dirty

.ng-invalid-required, .ng-valid-max-length, etc.

Use ‘novalidate’ attribute to stop HTML5 auto validation

Page 9: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Questions.. ?

Page 10: AngularJS training - Day 2 - Services, Controllers, Dependency Injection, Routing and Form Validation

Thank you!End of Day 2.