Understanding angular js

53
UNDERSTANDING KTM JS DEVELOPERS MEETUP EPISODE 2

Transcript of Understanding angular js

Page 1: Understanding angular js

UNDERSTANDING

KTMJS DEVELOPERS MEETUPEPISODE 2

Page 2: Understanding angular js

AYUSH SHRESTHA

Chief of Design – nLocateCo-Founder – lishn.com

Page 3: Understanding angular js
Page 4: Understanding angular js

What is Angular JS?Angular JS is an open-source JavaScript Framework maintained by Google for building Single Page Applications (SPAs). Its goal is to augment browser-based application with Model-View-Controller (MVC) capability.

Page 5: Understanding angular js
Page 6: Understanding angular js
Page 7: Understanding angular js
Page 8: Understanding angular js

What is Angular JS? Client-side JS Framework for SPA

Not just a single piece of a puzzle but full client side solution

Model-View-Controller framework

For front-end of your application

Steroids for your UI

Page 9: Understanding angular js

Core Features of Angular JSDISCLAIMER : THERE WILL BE CODE SNIPPETS IN THE SLIDES WHICH ARE ONLY FOR DEMO PURPOSE AND MIGHT NOT BE TOTALLY ACCURATE SYNTACTICALLY. IT IS ONLY MEANT TO GIVE A BRIEF OVERVIEW OF HOW THINGS WORK.

Page 10: Understanding angular js

Two-way Data BindingONE WAY DATA BINDING TWO WAY DATA BINDING

VIEW

TEMPLATE Model

One-time

Merge

TEMPLATE

VIEW

Model

CONTINUOUS UPDATE

Change in viewUpdates model

Change in modelUpdates view

Page 11: Understanding angular js

MVC Framework The whole application has 3 major components Model, View

and Controller

Model is the data layer

View is the UI layer

Controller is the logic layer

Actually, MVVM (Model-View-ViewModel) architecture

Page 12: Understanding angular js

Templates

Static Template

Dynamic Data

Final View

Page 13: Understanding angular js

Templates Templates are plain old HTML

Extended HTML Vocabulary to contain instructions on how to combine model data into view

Its NOT HTML string manipulation (one of the major

differences from other frameworks)

Page 14: Understanding angular js

Templates

ng-src

Page 15: Understanding angular js

Dependency Injection Built-in Dependency Injection Subsystem

Easier to understand and test

Modular Development

Just ask for things that you want to use (built-in or custom services)

Page 16: Understanding angular js

Dependency Injection

Page 17: Understanding angular js

Directives Extend HTML vocabulary to give

them superpowers

In-built and custom directives

Page 18: Understanding angular js

Core Concepts of Angular JS

Page 19: Understanding angular js

Modules Container for different parts of your application – controllers,

services, filters, etc.

Declaratively specify how an application is to be bootstrapped

Builds reusable component packages

Can be loaded in any order (or even in parallel)

Page 20: Understanding angular js

Modules

Page 21: Understanding angular js

Modules

Page 22: Understanding angular js

Modules

Page 23: Understanding angular js

Modules

Page 24: Understanding angular js

Modules A module for each feature

A module for reusable features

An application level module which will depend on above modules and will be auto-bootstrapped.

Page 25: Understanding angular js

Controllers Logic behind the view

Constructs the Model and publishes it to the View

Instantiate the ViewModel object or “$scope”

Set up the initial state of the $scope

Add behavior to $scope

Page 26: Understanding angular js

Controllers

Page 27: Understanding angular js

Writing Controllers

Page 28: Understanding angular js

Writing Controllers

Page 29: Understanding angular js

Writing Controllers

Page 30: Understanding angular js

View What the users see

HTML Template that is merged with the model and finally rendered into the browser DOM

Page 31: Understanding angular js

Model Data that is merged with the HTML template to produce

views

Page 32: Understanding angular js

Model Data that is merged with the HTML template to produce

views

Page 33: Understanding angular js

Scope An object that refers to the application’s data-model

$scope

Execution context for expression

Contains data, behaviors and other APIs to manage model mutation and events.

Page 34: Understanding angular js

Scope : Scope Hierarchy

Page 35: Understanding angular js

Scope : Scope HierarchyHello WorldHello John DoeHello RameshHello Suresh

Page 36: Understanding angular js

Scope : $watch API to observe model mutation

Page 37: Understanding angular js

Scope : $digest cycle A cycle that processes all watcher functions

Asynchronous Dirty Checking cycle

Not to be called directly, instead we use $apply

Page 38: Understanding angular js

Scope : $apply Explicitly evaluate expressions in angular from outside

angular

Executes $digest after expression evaluation

Page 39: Understanding angular js

Scope : Events $emit(‘somethingHappened’, args) – dispatches event

upwards the scope

$broadcast(‘somethingHappened’, args) – dispatches event downwards the scope

$on(‘somethingHappened’, listenerFunction) – listens to event fires and executes listener function.

Page 40: Understanding angular js

Filters Formats the value of an expression for display

Change form of data

Return a subset of list according to some rule

Page 41: Understanding angular js

Filters

Page 42: Understanding angular js

Filters : Custom

Page 43: Understanding angular js

Filters : Custom

Page 44: Understanding angular js

Services Injectable objects that can be used to organize and share code

and functions across the application

Could be used to share utility functions

Angular provides useful services like $http to make AJAX requests

We can also make custom services

Page 45: Understanding angular js

Services

Page 46: Understanding angular js

Directives Coolest feature of angular js

Markers on DOM elements (attributes, element names, class names, comment) that attach specified behaviors to that DOM element, or even transform the element

Superpowers for your DOM

Page 47: Understanding angular js

Directives : In-built directives Ng-app

Ng-bind

Ng-model

Ng-class

Ng-controller

Ng-show /Ng-Hide

Ng-if

Ng-switch

Page 48: Understanding angular js

Directives : Custom Directives

Page 49: Understanding angular js

Directives : Custom Directives

Page 50: Understanding angular js

Directives : Custom Directives

Page 51: Understanding angular js

That’s All For Today!! Routing using angular-ui-router

Scope Life Cycle

$resource

Services/Factories/Providers

Ng-include and $templateCache

Custom Directives

Page 52: Understanding angular js

One more thing…

Page 53: Understanding angular js

AYUSH SHRESTHA

fb.me/ShresthaAayush@AayushShrestha+AayushShresthaOriginal

[email protected]@gmail.com