get angularjs started!

37
Get Angular JS Started! Introduction to AngularJS Meetup Talk by Dmitry Ivashutin, april 2014

Upload: dzmitry-ivashutsin

Post on 28-Nov-2014

134 views

Category:

Technology


2 download

DESCRIPTION

'Introduction to AngularJS' presentation for ng-meetup at iTechArt (April 2014)

TRANSCRIPT

Page 1: Get AngularJS Started!

Get Angular JS

Started!

Introduction to AngularJS

Meetup Talk by Dmitry Ivashutin, april 2014

Page 2: Get AngularJS Started!
Page 3: Get AngularJS Started!

Presenters

Page 4: Get AngularJS Started!

What is not AngularJS?

➔ Not a JavaScript library, it’s a framework◆ it has no methods to call directly

➔ Not for DOM manipulation◆ but it has a limited jQuery stand-in inside (jqLite)

Page 5: Get AngularJS Started!

What is AngularJS?

➔ 100% JavaScript

➔ 100% Client side

➔ MVC || MVVM === MVW

Page 6: Get AngularJS Started!

Why is it ‘Angular’ and ‘ng’?

HTML has <> ‘ng’ sounds like ‘Angular’

Page 7: Get AngularJS Started!

Model-View-ViewModel

View

Model ViewModel

UI

Business Logic and Data

Presentation Logic

► user actions, commands► data bindings► notifications

► data access► update on change

Page 8: Get AngularJS Started!

Model-View-Controller

View

Model Controller

UI View:► renders model► sends User actions/events to controller

Controller:► defines app behaviors► maps actions/events to Model

Model:► represents data► handles Business Logic► notifies view on changes

User Interactions

Business Logic and Data

Page 9: Get AngularJS Started!

Key Features

Page 10: Get AngularJS Started!

Application

ng-app➔ auto-bootstrapping the application➔ one per page*➔ application scoping

<html data-ng-app></html>

Page 11: Get AngularJS Started!

Expressions

<body>1 + 2 = {{1 + 2}}

</body>1 + 2 = 3

HTML RESULT

http://jsfiddle.net/ae87/9uzkL/

Page 12: Get AngularJS Started!

Modules

You can think of a module as a container for the

different parts of your app – controllers, services, filters,

directives, etc.

var myApp =angular.module(‘myApp’, []);

Page 13: Get AngularJS Started!

Module configuring - Filter

// declare a module

var myAppModule =

angular.module(‘myApp’, []);

// configure the module. create a filter

myAppModule.filter(‘greet’, function() {

return function(name) {

return ‘Hello, ‘ + name + ‘!’;

};

});

<div>

{{ ‘World’ | greet }}

</div>

Hello, World!

HTMLJS

RESULT

http://jsfiddle.net/ae87/AXrxb/

Page 14: Get AngularJS Started!

Directives

<input data-ng-model=”name” />

At a high level, directives are markers on a DOM element (such as an

attribute, element name, or CSS class) that tell AngularJS's HTML compiler ($compile) to attach a

specified behavior to that DOM element or

even transform the DOM element and its children.

Page 15: Get AngularJS Started!

Controllers

// declare a module

var myApp =

angular.module(‘myApp’, []);

// configure the module. add controller

myApp.controller(‘MeetupController’,

[‘$scope’, function($scope) {

$scope.greetings = ‘Welcome!’;

}]);

<div data-ng-controller=

”MeetupController”>

{{ greetings }}

</div>

Welcome!

JS HTML

RESULT

http://jsfiddle.net/ae87/R5z7p/

Page 16: Get AngularJS Started!

Use controllers to:

➔ Set up the initial state of the $scope object.

➔ Add behavior to the $scope object.

Page 17: Get AngularJS Started!

Do NOT use controllers to:

➔ Manipulate DOM

➔ Format input

➔ Filter output

➔ Share code or state across controllers

➔ Manage life-cycle of other components

Page 18: Get AngularJS Started!

Scope

// declare a module

var myApp =

angular.module(‘myApp’, []);

// add value to $scope object

myApp.controller(‘MeetupController’,

[‘$scope’, function($scope) {

$scope.greetings = ‘Welcome!’;

}]);

➔ Viewmodel like object (MVVM)

➔ Model presentation (MVC)

JS

Page 19: Get AngularJS Started!

What are scopes?

➔ mimics DOM structure*

◆ parent inheritance

◆ child isolation

➔ ‘$watch’s model mutations

➔ ‘$apply’ies model changes on view

Page 20: Get AngularJS Started!

Dependency Injection

// declare a module

var myApp =

angular.module(‘myApp’, []);

// inject reference to $scope object

myApp.controller(‘MeetupController’,

[‘$scope’, function($scope) {

$scope.greetings = ‘Welcome!’;

}]);

➔ inline array annotation

➔ $inject property annotation

➔ implicitly from the function parameters names

JS

Page 21: Get AngularJS Started!

Directives: ng-repeat

// adding array for iteration over it

myApp.controller(‘MeetupController’,

[‘$scope’, function($scope) {

$scope.attendees = [

{ name: ‘Alexey’, room: 521 },

{ name: ‘Olga’, room: 504 },

{ name: ‘Sergey’, room: 522 },

];

}]);

JS <ul>

<li data-ng-repeat=”a in attendees”>

{{ a.name }} ( {{ a.room }} )

</li>

</ul>

● Alexey ( 521 )● Olga ( 504 )● Sergey ( 522 )

HTML

RESULT

http://jsfiddle.net/ae87/q8X9g/

Page 22: Get AngularJS Started!

Directives: ng-class

<ul>

<li data-ng-repeat=”a in attendees”

data-ng-class=

”{red:a.room==504}”>

{{ a.name }}} ( {{ a.room }} )

</li>

</ul>

HTML.red {

color: DarkRed;

font-weight: bold;

}

● Alexey ( 521 )● Olga ( 504 )● Sergey ( 522 )

CSS

RESULT

http://jsfiddle.net/ae87/yW4mc/

Page 23: Get AngularJS Started!

Directives: ng-class

<ul>

<li data-ng-repeat="s in speakers"

data-ng-class=

"{true: 'blue', false: 'green'}

[s.room==522]">

{{ s.name }} ( {{ s.room }} )

</li>

</ul>

HTML .blue{

color: DarkBlue; font-weight: bold;

}

.green {

color: DarkGreen; font-weight: bold;

}

● Dmitry ( 522 )● Vladimir ( 521 )

CSS

RESULT

http://jsfiddle.net/ae87/cc8Q4/

Page 24: Get AngularJS Started!

Directives: ng-click

myApp.controller(‘MeetupController’,

[‘$scope’, function($scope) {

$scope.addAttendee = function(e){

$scope.attendees.push({

name: $scope.name,

room: $scope.room

});

};

}]);

JS<input data-ng-model=”name” type=”text”/>

<input data-ng-model=”room” type=”text”/>

<button data-ng-click=”addAttendee($event)”>

Add Attendee

</button>

● Alexey ( 521 )● Olga ( 504 )● Sergey ( 522 )● Denis ( 500 )

HTML

RESULT

http://jsfiddle.net/ae87/QNGq4/

Page 25: Get AngularJS Started!

Directives: ng-show

<input data-ng-show=”canEdit” />

The ngShow directive shows or hides the given HTML element

based on the expression provided to the ngShow

attribute. The element is shown or hidden by removing or adding the ng-hide CSS class onto the element. The .ng-hide CSS class is predefined in

AngularJS and sets the display style to none

using an !important flag.

Page 26: Get AngularJS Started!

Directives: ng-if

The ngIf directive

removes or recreates a portion of the DOM

tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is

reinserted into the DOM.

<div data-ng-if=”canView” />

Page 27: Get AngularJS Started!

Dependency Injection objects

➔ Services:◆ Value, Factory, Service, Provider, Constant,

Decorator*

➔ Special purpose objects:◆ Controller, Directive, Filter, Animation

Page 28: Get AngularJS Started!

Services

➔ Injectable objects

➔ Singletons

➔ Lazy instantiated

Page 29: Get AngularJS Started!

Factory Recipe

myApp.factory(‘alerter’, function () {

var alerter = {};

alerter.sayHello = function () {

alert(‘Hello!’);

}

alerter.sayGoodbye = function () {

alert(‘Goodbye!’);

}

return alerter;

});

JSmyApp.controller(‘MeetupController’,

[‘$scope’, ‘alerter’,

function($scope, alerter) {

$scope.alerter = alerter;

}]);

<button data-ng-click=”alerter.sayHello()”>

Say Hello

</button>

<button data-ng-click=”alerter.sayGoodbye()”>

Say Goodbye

</button>

JS

HTML

http://jsfiddle.net/ae87/9x7Aq/1/

Page 30: Get AngularJS Started!

Service Recipe

myApp.service(‘alerter’, function () {

this.sayHello = function(){

alert(‘Hello!’);

}

this.sayGoodbye = function(){

alert(‘Goodbye!’);

}

});

JSmyApp.controller(‘MeetupController’,

[‘$scope’, ‘alerter’,

function($scope, alerter) {

$scope.alerter = alerter;

}]);

<button data-ng-click=”alerter.sayHello()”>

Say Hello

</button>

<button data-ng-click=”alerter.sayGoodbye()”>

Say Goodbye

</button>

JS

HTML

http://jsfiddle.net/ae87/9x7Aq/2/

Page 31: Get AngularJS Started!

Provider Recipe

myApp.provider(‘alerter’, function () {

var name = ‘Dear Guest’;

this.setGuestName = function(newName) {

name = newName;

};

this.$get = [/*you can DI in provider here*/

function(){

var alerter = {};

// your code using name variable

return alerter;

}];

});

JSmyApp.controller(‘MeetupController’,

[‘$scope’, ‘alerter’,

function($scope, alerter) {

$scope.alerter = alerter;

}]);

<button data-ng-click=”alerter.sayHello()”>

Say Hello

</button>

<button data-ng-click=”alerter.sayGoodbye()”>

Say Goodbye

</button>

JS

HTML

http://jsfiddle.net/ae87/9x7Aq/3/

Page 32: Get AngularJS Started!

myApp.value(‘apiUserId’, ‘Af8as131A’);

myApp.controller(‘mapsController’, [

‘$scope’, ‘apiUserId’,

function($scope, apiUserId){

$scope.apiUserId = apiUserId;

}]);

Value Recipe

JS

➔ a string, a number, an array, an object or a function

➔ cannot be injected in config section

➔ can be overridden by decorator

Page 33: Get AngularJS Started!

myApp.constant(‘apiUserId’, ‘AWjd812’);

myApp.controller(‘mapsController’, [

‘$scope’, ‘apiUserId’,

function($scope, apiUserId){

$scope.apiUserId = apiUserId;

}]);

Constant Recipe

JS

➔ a string, a number, an array, an object or a function

➔ can be injected in config section

➔ cannot be overridden by decorator

Page 34: Get AngularJS Started!

Providers

Features / Recipe type Factory Service Value Constant Provider

can have dependencies yes yes no no yes

uses type friendly injection no yes yes* yes* no

object available in config phase

no no no yes yes**

can create functions/primitives

yes no yes yes yes

Page 35: Get AngularJS Started!

Demoable

http://jsfiddle.net/ae87/4rckY/

Page 36: Get AngularJS Started!

What else?

Forms and validation

Routing

Configuration

$http

$q and promises

Deeper about directives

$broadcast and $emit

and a large list of other interesting things is coming next time …

Page 37: Get AngularJS Started!

Questions?