KEMBAR78
Angular Intermediate | PDF
AngularJs
Intermediate
Who we are
Daniele Bertella
@_denb
Gianfranco Garipoli
@gianfra__
LinkMe SRL
@LinkMeSRL
Target
People who have already some experience with
angular and wanted to see more feature.
Topics
● Integration with RESTful API
● Code modularization
● Client side validations
● Custom directive
● Testing
Slack - Join us on MEAN milan
If you are not there already: http://meanmilan.herokuapp.com/
https://meanmilan.slack.com/
Ninja time!
https://github.com/linkmesrl/ngIntermediate
What we are going to do
What is it?
Back end: Node.js + Express + MongoDb
Front end: Angular Js
1° Lesson
$http, $resource
Angular $http
Angular $http - shortcut
Angular $http - in the service
Angular $http - in controller
Ok let’s go $resource
$resource - Usage
$resource(url, [paramDefaults], [actions], options);
https://docs.angularjs.org/api/ngResource/service/$resource
$resource - Let’s create the model
$resource
Each of the model instances will have methods,
which could be used for the different CRUD
operation.
In one line of code.
It’s not free!
ngResource should be installed in your app
$resource - Default method
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
$resource - How to use it - Query
// Get all
var ninjas = Ninja.query();
$resource - How to use it - Get
// get single
Ninja.get({ id: 1}, function (ninja) {
...
});
// get single using $promise
Ninja.get({ id: 1}).$promise
.then(...);
$resource - Handling returned data
// $save, $remove ($delete)
ninja.$save();
ninja.$remove();
Let’s code
● Create a service to incapsulate the $resource
● Create 2 controllers to handle the requests
● Connect the views with their controllers
$resource - wrap it into a service
.service('Ninja', function($resource){
var Ninja = $resource('http://192.168.105.130:3000/api/ninja/:
_id', {_id: '@_id'});
return Ninja;
});
$resource - Controller, load ninjas and go on
.controller('NinjaCtrl', function($scope, Ninja){
$scope.ninjas = [];
// TODO GetAll, CreateNew, Update, Delete
$scope.loadNinja = function(){
$scope.ninjas = Ninja.query();
};
...
});
$resource - Custom methods
{
action1: {method:?, params:?, isArray:?, headers:?, ...},
action2: {method:?, params:?, isArray:?, headers:?, ...},
...
}
$resource - Custom method example
var Ninja = $resource('http://127.0.0.1:3000/api/ninja/:_id',
{_id: '@_id'}, {
count: {method:'GET', url: 'http://127.0.0.1:
3000/api/ninja/count'}
});
// our custom method is mapped as well.
Ninja.count();
2° Lesson
form validation, directive
Let’s use the built in
Angular form validation
ngForm - validation
http://codepen.io/sevilayha/full/xFcdI/
ngForm - validation - Property | classes
● $valid | .ng-valid
● $invalid | .ng-invalid
● $pristine | .ng-pristine
● $dirty | .ng-dirty
● $touched | .ng-touched
ngForm - validation - Other properties
● $submitted
● $error
With $error property we can target every different errors in the form:
myForm.$error.required
myForm.$error.minlength
myForm.$error.maxlength
ngForm - validation - access properties
● formName.property
ex: “createForm.$valid”
● formName.inputName.property
ex: “createForm.age.$pristine”
ngForm - input directive (with ng-model)
https://docs.angularjs.org/api/ng/directive/input
ngForm - custom errors
Directives
Directives makes HTML interactive by attaching event listeners to elements
and/or transforming the DOM.
Angular directive
● ng-model
● ng-repeat
● ng-show
<input ng-model=”name” />
<div ng-repeat=”item in items”></div>
Angular custom directive
<ng-intermediate />
or
<div ng-intermediate></div>
simple object
configuration
● no scope => parent’s scope.
● scope: true => child scope
● scope: {} => isolated scope
With isolate scope:
● @ => Attribute string binding
● = => Two-way model binding
● & => Callback method binding
directive scope
directive scope example
<ng-intermediate cheering=”{{hello}}” name=”class” />
directive isolated scope explained
● @ binding is for passing strings. These strings support {{}} expressions for interpolated values.
For example: . The interpolated expression is evaluated against directive's parent scope.
● = binding is for two-way model binding. The model in parent scope is linked to the model in the
directive's isolated scope. Changes to one model affects the other, and vice versa.
● & binding is for passing a method into your directive's scope so that it can be called within your
directive. The method is pre-bound to the directive's parent scope, and supports arguments. For
example if the method is hello(name) in parent scope, then in order to execute the method from
inside your directive, you must call $scope.hello({name:'world'})
directive link function
The link function is mainly used for attaching event
listeners to DOM elements, watching model properties for
changes, and updating the DOM.
directive link function attributes
● scope – The scope passed to the directive.
● el – The jQLite (a subset of jQuery) wrapped
element on which the directive is applied.
● attr – An object representing normalized
attributes attached to the element on which
the directive is applied.
directive link function example
directive require
Directive Definition Object
● https://docs.angularjs.
org/api/ng/service/$compile
● https://docs.angularjs.
org/guide/directive
Let’s build our
custom validation
Register a custom directive
Configure our custom directive
Add the validation logic
3° Lesson
Modularization
Path to Angular 2
What is a Module?
You can think of a module as a container for the
different parts of your app
Let’s modularize the app
App structure - Angular 1.x
App structure - Angular > 1.3
Ideas?
Let’s modularize the app
ninja-list
ninja-tile
ninja-handle
Let’s start by creating component as a directive.
We will use directive definition object where we
declare controller, controllerAs, templateUrl, and
bindToController properties.
Directive Definition Object
https://docs.angularjs.org/guide/directive
ControllerAs
ControllerAs syntax enables controller to live and
do their job without being bound (only) on $scope.
bindToController (new in Angular 1.3.X)
When used, properties from isolated scope will be
automatically bound to controller’s this.
Example
Let’s do it
together
Exercise App structure
Exercise ninja.module.js
Exercise <ninja-list>
● Create a new module with a new routing
● Create one component for every part of the app
● Every component should have one controller
and one view
Exercise
4° Lesson
test test test
Testing Theory
What tests are?
Automated software testing
is a process in which software tools
execute pre-scripted tests
on a software application
before it is released into production.
Tests Typologies
Unit Tests
A unit test is an automated piece of
code that invokes a unit of work in
the system and then checks a single
assumption about the behavior of
that unit of work.
➔ Has full control over all the pieces running (Use mocks)
➔ Can be run in any order if part of many other tests
➔ Runs in memory (no DB or File access, for example)
➔ Consistently returns the same result
➔ Tests a single logical concept in the system
➔ Readable and Maintainable
End to End Tests
End-to-end testing is a methodology
used to test whether the flow of an
application is performing as designed
from start to finish.
➔ Use the real code (no mocks)
➔ Test Integration between components
➔ Test a workflow (eg: Registration, Profile Update, …)
➔ Run in a real Environment
What to be tested?
Unit Tests
➔ Complex Algorithm
➔ Shared Functions
➔ Core Functions
E2E Tests
➔ Complex Workflows
➔ Business Value
➔ Smoke Test
Angular Testing Toolset
Unit Tests
➔ Karma
➔ Jasmine
➔ ngMock, $inject,
$template
E2E Tests
➔ Protractor
➔ Jasmine
How test are structured
A suite that describe what is going to be tested
Some specs that verify a behaviour
Some expectations that assert a result
Setup and Cleanup before and after each test
How test are structured
Suites - Describe
Describe blocks:
➔ Used to wrap spec from the same component
➔ Can be nested
➔ Should be verbose
➔ Can be skipped (xdescribe)
Spec - It
It blocks:
➔ Used to wrap expectations for the same unit
➔ Can NOT be nested
➔ Should be verbose
➔ Can be skipped (xit)
➔ Should test only one behaviour
Setup and Cleanup
before, beforeEach, after, afterEach blocks:
➔ Used to setup/cleanup the test environment
➔ Mock the data
➔ Mock external Unit
➔ Mock the backend
➔ Can NOT be nested
Let’s test our
components
Our modularized app
ninja-list
ninja-tile
ninja-handle
Getting Started
Unit Tests - Environment Setup
Setting up Karma Environment
In order to execute Unit Tests we need:
➔ A test runner (Karma)
➔ An assertion library (Jasmine)
➔ A browser in which run the tests (PhantomJs)
➔ A configuration File
➔ One spec (at least)
➔ A script to run the test (npm test)
Setting up Karma Environment
npm install
jasmine-core
karma
karma-jasmine
karma-mocha-reporter karma-ng-html2js-
preprocessor karma-phantomjs-launcher
phantomjs
--save-dev
Karma CLI
Optionally Install:
npm install -g karma-cli
Then, you can run Karma simply by karma from anywhere
and it will always run the local version.
Karma Configuration File
By default the configuration file is called:
karma.conf.js
http://karma-runner.github.io/0.8/config/configuration-file.html
karma init is an helper to generate this file
Karma Configuration File
What’s inside:
➔ Base path: path that will be used to resolve all patterns
➔ Assertion framework: (Jasmine) complete list here
➔ Files to be loaded: Spec and application files
➔ Browser: Test environment
➔ Preprocessors*: Babel, Html2js, ...
➔ Reporters: Formatter for the test output (Mocha)
Npm Test
It’s a common practice to define a script called:
npm test
"scripts": {"test": "karma start"}
Exercise
ninja-list -> What to test?
● Variable in scope
● Function in scope
● Html render
ninja-list -> show me the code
$httpBackend
Fake HTTP backend implementation suitable for
unit testing applications that use the $http
service.
https://docs.angularjs.org/api/ngMock/service/$httpBackend
$httpBackend -> How we use it
var serverMock = [{'age': 55, 'name': 'Daniele', '_id': '0'}, {'age': 35, 'name': 'Marco', '_id': '1'}];
Flushing HTTP requests
flush() method, allows the test to explicitly flush
pending requests.
This preserves the async api of the backend, while
allowing the test to execute synchronously.
ninja-list (beforeEach) final
The end
questions?
Thanks
Daniele Bertella
@_denb
Gianfranco Garipoli
@gianfra__
LinkMe SRL
@LinkMeSRL
Links
*** If you are not there already: http://meanmilan.herokuapp.com/
● http://linkme.it/
● https://github.com/linkmesrl/ngIntermediate
● https://meanmilan.slack.com/ ***

Angular Intermediate

  • 1.
  • 2.
    Who we are DanieleBertella @_denb Gianfranco Garipoli @gianfra__ LinkMe SRL @LinkMeSRL
  • 3.
    Target People who havealready some experience with angular and wanted to see more feature.
  • 4.
    Topics ● Integration withRESTful API ● Code modularization ● Client side validations ● Custom directive ● Testing
  • 5.
    Slack - Joinus on MEAN milan If you are not there already: http://meanmilan.herokuapp.com/ https://meanmilan.slack.com/
  • 6.
  • 7.
    What we aregoing to do
  • 8.
    What is it? Backend: Node.js + Express + MongoDb Front end: Angular Js
  • 9.
  • 10.
  • 11.
  • 12.
    Angular $http -in the service
  • 13.
    Angular $http -in controller
  • 14.
    Ok let’s go$resource
  • 15.
    $resource - Usage $resource(url,[paramDefaults], [actions], options); https://docs.angularjs.org/api/ngResource/service/$resource
  • 16.
    $resource - Let’screate the model
  • 17.
    $resource Each of themodel instances will have methods, which could be used for the different CRUD operation. In one line of code. It’s not free! ngResource should be installed in your app
  • 18.
    $resource - Defaultmethod 'get': {method:'GET'}, 'save': {method:'POST'}, 'query': {method:'GET', isArray:true}, 'remove': {method:'DELETE'}, 'delete': {method:'DELETE'}
  • 19.
    $resource - Howto use it - Query // Get all var ninjas = Ninja.query();
  • 20.
    $resource - Howto use it - Get // get single Ninja.get({ id: 1}, function (ninja) { ... }); // get single using $promise Ninja.get({ id: 1}).$promise .then(...);
  • 21.
    $resource - Handlingreturned data // $save, $remove ($delete) ninja.$save(); ninja.$remove();
  • 22.
    Let’s code ● Createa service to incapsulate the $resource ● Create 2 controllers to handle the requests ● Connect the views with their controllers
  • 23.
    $resource - wrapit into a service .service('Ninja', function($resource){ var Ninja = $resource('http://192.168.105.130:3000/api/ninja/: _id', {_id: '@_id'}); return Ninja; });
  • 24.
    $resource - Controller,load ninjas and go on .controller('NinjaCtrl', function($scope, Ninja){ $scope.ninjas = []; // TODO GetAll, CreateNew, Update, Delete $scope.loadNinja = function(){ $scope.ninjas = Ninja.query(); }; ... });
  • 25.
    $resource - Custommethods { action1: {method:?, params:?, isArray:?, headers:?, ...}, action2: {method:?, params:?, isArray:?, headers:?, ...}, ... }
  • 26.
    $resource - Custommethod example var Ninja = $resource('http://127.0.0.1:3000/api/ninja/:_id', {_id: '@_id'}, { count: {method:'GET', url: 'http://127.0.0.1: 3000/api/ninja/count'} }); // our custom method is mapped as well. Ninja.count();
  • 27.
  • 28.
    Let’s use thebuilt in Angular form validation
  • 29.
  • 30.
    ngForm - validation- Property | classes ● $valid | .ng-valid ● $invalid | .ng-invalid ● $pristine | .ng-pristine ● $dirty | .ng-dirty ● $touched | .ng-touched
  • 31.
    ngForm - validation- Other properties ● $submitted ● $error With $error property we can target every different errors in the form: myForm.$error.required myForm.$error.minlength myForm.$error.maxlength
  • 32.
    ngForm - validation- access properties ● formName.property ex: “createForm.$valid” ● formName.inputName.property ex: “createForm.age.$pristine”
  • 33.
    ngForm - inputdirective (with ng-model) https://docs.angularjs.org/api/ng/directive/input
  • 34.
  • 35.
    Directives Directives makes HTMLinteractive by attaching event listeners to elements and/or transforming the DOM.
  • 36.
    Angular directive ● ng-model ●ng-repeat ● ng-show <input ng-model=”name” /> <div ng-repeat=”item in items”></div>
  • 37.
    Angular custom directive <ng-intermediate/> or <div ng-intermediate></div> simple object configuration
  • 38.
    ● no scope=> parent’s scope. ● scope: true => child scope ● scope: {} => isolated scope With isolate scope: ● @ => Attribute string binding ● = => Two-way model binding ● & => Callback method binding directive scope
  • 39.
    directive scope example <ng-intermediatecheering=”{{hello}}” name=”class” />
  • 40.
    directive isolated scopeexplained ● @ binding is for passing strings. These strings support {{}} expressions for interpolated values. For example: . The interpolated expression is evaluated against directive's parent scope. ● = binding is for two-way model binding. The model in parent scope is linked to the model in the directive's isolated scope. Changes to one model affects the other, and vice versa. ● & binding is for passing a method into your directive's scope so that it can be called within your directive. The method is pre-bound to the directive's parent scope, and supports arguments. For example if the method is hello(name) in parent scope, then in order to execute the method from inside your directive, you must call $scope.hello({name:'world'})
  • 41.
    directive link function Thelink function is mainly used for attaching event listeners to DOM elements, watching model properties for changes, and updating the DOM.
  • 42.
    directive link functionattributes ● scope – The scope passed to the directive. ● el – The jQLite (a subset of jQuery) wrapped element on which the directive is applied. ● attr – An object representing normalized attributes attached to the element on which the directive is applied.
  • 43.
  • 44.
  • 45.
    Directive Definition Object ●https://docs.angularjs. org/api/ng/service/$compile ● https://docs.angularjs. org/guide/directive
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
    What is aModule? You can think of a module as a container for the different parts of your app
  • 52.
  • 53.
    App structure -Angular 1.x
  • 54.
    App structure -Angular > 1.3
  • 55.
  • 56.
    Let’s modularize theapp ninja-list ninja-tile ninja-handle
  • 57.
    Let’s start bycreating component as a directive. We will use directive definition object where we declare controller, controllerAs, templateUrl, and bindToController properties.
  • 58.
  • 59.
    ControllerAs ControllerAs syntax enablescontroller to live and do their job without being bound (only) on $scope.
  • 60.
    bindToController (new inAngular 1.3.X) When used, properties from isolated scope will be automatically bound to controller’s this.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
    ● Create anew module with a new routing ● Create one component for every part of the app ● Every component should have one controller and one view Exercise
  • 67.
  • 68.
  • 69.
    What tests are? Automatedsoftware testing is a process in which software tools execute pre-scripted tests on a software application before it is released into production.
  • 70.
    Tests Typologies Unit Tests Aunit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work. ➔ Has full control over all the pieces running (Use mocks) ➔ Can be run in any order if part of many other tests ➔ Runs in memory (no DB or File access, for example) ➔ Consistently returns the same result ➔ Tests a single logical concept in the system ➔ Readable and Maintainable End to End Tests End-to-end testing is a methodology used to test whether the flow of an application is performing as designed from start to finish. ➔ Use the real code (no mocks) ➔ Test Integration between components ➔ Test a workflow (eg: Registration, Profile Update, …) ➔ Run in a real Environment
  • 71.
    What to betested? Unit Tests ➔ Complex Algorithm ➔ Shared Functions ➔ Core Functions E2E Tests ➔ Complex Workflows ➔ Business Value ➔ Smoke Test
  • 72.
    Angular Testing Toolset UnitTests ➔ Karma ➔ Jasmine ➔ ngMock, $inject, $template E2E Tests ➔ Protractor ➔ Jasmine
  • 73.
    How test arestructured A suite that describe what is going to be tested Some specs that verify a behaviour Some expectations that assert a result Setup and Cleanup before and after each test
  • 74.
    How test arestructured
  • 75.
    Suites - Describe Describeblocks: ➔ Used to wrap spec from the same component ➔ Can be nested ➔ Should be verbose ➔ Can be skipped (xdescribe)
  • 76.
    Spec - It Itblocks: ➔ Used to wrap expectations for the same unit ➔ Can NOT be nested ➔ Should be verbose ➔ Can be skipped (xit) ➔ Should test only one behaviour
  • 77.
    Setup and Cleanup before,beforeEach, after, afterEach blocks: ➔ Used to setup/cleanup the test environment ➔ Mock the data ➔ Mock external Unit ➔ Mock the backend ➔ Can NOT be nested
  • 78.
  • 79.
  • 80.
    Getting Started Unit Tests- Environment Setup
  • 81.
    Setting up KarmaEnvironment In order to execute Unit Tests we need: ➔ A test runner (Karma) ➔ An assertion library (Jasmine) ➔ A browser in which run the tests (PhantomJs) ➔ A configuration File ➔ One spec (at least) ➔ A script to run the test (npm test)
  • 82.
    Setting up KarmaEnvironment npm install jasmine-core karma karma-jasmine karma-mocha-reporter karma-ng-html2js- preprocessor karma-phantomjs-launcher phantomjs --save-dev
  • 83.
    Karma CLI Optionally Install: npminstall -g karma-cli Then, you can run Karma simply by karma from anywhere and it will always run the local version.
  • 84.
    Karma Configuration File Bydefault the configuration file is called: karma.conf.js http://karma-runner.github.io/0.8/config/configuration-file.html karma init is an helper to generate this file
  • 85.
    Karma Configuration File What’sinside: ➔ Base path: path that will be used to resolve all patterns ➔ Assertion framework: (Jasmine) complete list here ➔ Files to be loaded: Spec and application files ➔ Browser: Test environment ➔ Preprocessors*: Babel, Html2js, ... ➔ Reporters: Formatter for the test output (Mocha)
  • 86.
    Npm Test It’s acommon practice to define a script called: npm test "scripts": {"test": "karma start"}
  • 87.
  • 88.
    ninja-list -> Whatto test? ● Variable in scope ● Function in scope ● Html render
  • 89.
    ninja-list -> showme the code
  • 90.
    $httpBackend Fake HTTP backendimplementation suitable for unit testing applications that use the $http service. https://docs.angularjs.org/api/ngMock/service/$httpBackend
  • 91.
    $httpBackend -> Howwe use it var serverMock = [{'age': 55, 'name': 'Daniele', '_id': '0'}, {'age': 35, 'name': 'Marco', '_id': '1'}];
  • 92.
    Flushing HTTP requests flush()method, allows the test to explicitly flush pending requests. This preserves the async api of the backend, while allowing the test to execute synchronously.
  • 93.
  • 94.
  • 95.
  • 96.
    Links *** If youare not there already: http://meanmilan.herokuapp.com/ ● http://linkme.it/ ● https://github.com/linkmesrl/ngIntermediate ● https://meanmilan.slack.com/ ***