angular 2 weekend light
TRANSCRIPT
![Page 1: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/1.jpg)
CoWork SouthBay 25 + 26 February 2017
Angular 2.0 Weekend Workshop
![Page 2: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/2.jpg)
Troy Miles• Troy Miles aka the RocknCoder
• Over 38 years of programming experience
• Speaker and author
• bit.ly/rc-jquerybook
• @therockncoder
• Now a lynda.com Author!
![Page 3: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/3.jpg)
Build Mobile Apps!
• Develop mobile apps with Ionic and AngularJS
• Learn the Ionic CLI
• Fetch data via ajax
• Deploy your app to Android & iOS
• bit.ly/ionicvideo
![Page 4: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/4.jpg)
![Page 5: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/5.jpg)
![Page 6: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/6.jpg)
April1&2
![Page 7: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/7.jpg)
–Alejandro (Sicario 2015)
“Nothing will make sense to your American ears, and you will doubt everything that we do,
but in the end you will understand”
![Page 8: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/8.jpg)
Agenda: Day One• Overview
• Angular CLI
• Webpack
• Modern JavaScript
• TypeScript
• Components / Views
• Router
• Data Binding
• Dependency Injection
• Services
![Page 9: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/9.jpg)
Agenda: Day Two• HTTP
• Forms
• Pipes
• Directives
• RxJS
• Unit Testing
• Angular Material
• Summary
![Page 10: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/10.jpg)
Tools• install git
• install node.js
• upgrade npm npm install npm -g
• install RxJS npm install -g rxjs
• install Angular CLI npm install -g @angular/cli
• install Firebase: npm install -g firebase-tools
![Page 11: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/11.jpg)
Check Versionsapp command my version
git git —version 2.11.0
node.js node -v v7.4.0
npm npm —v 4.0.5
angular cli ng -v 1.0.0-beta.32.3
firebase firebase version 3.3.0
![Page 12: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/12.jpg)
Application Root Directory
• All of the commands, for all of the tools are designed work on the application root directory
• If used anywhere else bad things will happen
• be sure you are in the app root
• double check that you are in the app root
![Page 13: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/13.jpg)
Angular CLITool Command
New App ng new <app-name>
Web Server ng serve
Unit Test ng test
End to End Test ng e2e
Dev Build ng build dev
Production Build ng build prod
![Page 14: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/14.jpg)
Create New ComponentsComponent CommandClass ng g class my-new-classComponent ng g component my-new-componentDirective ng g directive my-new-directiveEnum ng g enum my-new-enumInterface ng g interface my-new-interfaceModule ng g module my-modulePipe ng g pipe my-new-pipeService ng g service my-new-service
![Page 15: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/15.jpg)
naming convention• Angular apps are usually built using lowercase
kebab naming convention
• words are lowercase, dash as separator
• final word is the thing’s type, a dot is its separator
• if the thing is a class, no final word
• contact-details.component.ts
![Page 16: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/16.jpg)
Webpack
• Open source module bundler
• Replaces System.JS
• Works with JS, CSS, and HTML
• Minifies, Concatenates, and Bundles
![Page 17: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/17.jpg)
Firebase commandsCommand What?
firebase login logins into your firebase account
firebase init sets up a new firebase project
firebase deploy deploys your app to firebase hosting
firebase list lists all your firebase projects
firebase open open project resources
firebase use —add adds the current
![Page 18: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/18.jpg)
Firebase Steps1. Create project in Firebase
2. Go to application root in terminal/ cmd prompt
3. firebase login
4. firebase init (connects app to firebase project)
5. ng build prod (creates “dist" directory)
6. firebase deploy
7. Refresh cache (Chrome: cmd+shift+R / ctrl+shift+R )
![Page 19: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/19.jpg)
ECMAScript VersionsVersion Date
ES1 June 1997
ES2 June 1998
ES3 December 1999
ES4 DOA 2006
ES5 December 2009
ES6/ES2015 June 2015
ES2016 2016
![Page 20: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/20.jpg)
2009 • ES3 ruled the land, it is 10 years old
• ES4 was abandoned due to infighting
• ES5 was in development, strict mode still a dream
• FireFox 3.5 is the most popular browser
• AngularJS is born
![Page 21: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/21.jpg)
AngularJS
• Created by Miško Hevery and Adam Abrons in 2009
• JavaScript MVC
• Declarative programming for UI
• Imperative programming for business logic
![Page 22: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/22.jpg)
AngularJS Main Parts• Module
• Views/Controllers
• Services
• Directives
• Filters
![Page 23: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/23.jpg)
Since 2009• Two new versions of JavaScript (ES5 & ES6)
• Lots of new browser capabilities
• Transpilers like TypeScript and Babel
• Cool libraries like RxJS and React
• No longer necessary for Angular to do everything
![Page 24: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/24.jpg)
Getting started• npm install typescript -g
• git clone https://github.com/Rockncoder/quizzer-start.git
• cd quizzer-start
• npm install
• npm start
• refresh browser
![Page 25: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/25.jpg)
npm
![Page 26: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/26.jpg)
npm• We need Nodejs for its package manager, npm
• NPM requires a file, package.json, in the app root
• We use npm for a lot of things
• npm officially doesn’t stand for anything
• https://www.npmjs.com/
![Page 27: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/27.jpg)
package.json• name - what this app is called, for npm it must be
• version - the current version number
• description - what this package is all about
• author - who wrote it
• repository - where it lives
![Page 28: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/28.jpg)
package.json
• license - type of license granted to users of the package
• scripts - commands
• dependencies - packages for production
• devDependencies - packages for development
![Page 29: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/29.jpg)
version definitions• “^1.8.9"
• 1 - is the major number
• 8 - is the minor number
• 9 - is the patch number
• patch number is not required
![Page 30: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/30.jpg)
version symbols• "1.8.9" - must exactly match version
• ">1.8.9" - must be greater than version
• ">=1.8.9", "<1.8.9", "<=1.8.9"
• "^1.8.9"- allows changes to the minor & patch
• "~1.8.9" - allows changes to the patch
• "1.8.*" - a wild card which stands for any value here
![Page 31: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/31.jpg)
install• The install adds a package to your app
• npm install <package name> [—save | —save-dev]
• npm i <package name> [-d | -D]
• —save: saves info to dependencies
• —save-dev: saves info to devDependencies
![Page 32: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/32.jpg)
run-script
• npm run-script <command> <args>
• npm run <command> <args> (shortcut)
• to abort use control-c
![Page 33: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/33.jpg)
What’s in our package.json?• angular2 - Needs no explanation
• es6-promise - A polyfill for ES6 promises
• es6-shim - A library that aligns JavaScript more closely to ES6
• reflect-metadata - Polyfill for ES7 decorators
• rxjs - a library for asynchronous and event based programming
• systemjs - a module loading system
• zone.js - for change detection
![Page 34: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/34.jpg)
What’s in our package.json?
• concurrently - runs multiple commands concurrently
• lite-server - a light development node server
• typescript - a language for application scale JS
• typings - the typescript definition manager
![Page 35: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/35.jpg)
Where can you look up npm info?
• https://angular.io/
• https://www.npmjs.com/
• https://angularjs.org/
• https://nodejs.org/
![Page 36: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/36.jpg)
What’s difference between the ‘^’ and the ‘~’ in packages?
A. The ~ means any version greater than this one, while the ^ means any version less than this one
B. The ~ is a wild card but the ^ means only this version
C. The ~ allows different patch number, while the ^ allows changes to both minor and patch numbers
![Page 37: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/37.jpg)
Angular 2
![Page 38: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/38.jpg)
It’s not AngularJS 2• ES5
• ES6
• CoffeeScript
• Dart
• TypeScript
![Page 39: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/39.jpg)
Deleted from Angular 2• $scope
• Data Definition Object
• angular module
• controllers
• jqLite / jQuery
![Page 40: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/40.jpg)
Main Parts• Module
• Component
• Template
• Data binding
• Metadata
• Service
• Directive
• Dependency Injection
![Page 41: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/41.jpg)
ES5
![Page 42: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/42.jpg)
strict mode• 'use strict’; or "use strict;"
• First line of file or function
• Can break working code!!
• More stringent checking
• Enables ES5 features
![Page 43: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/43.jpg)
strict mode• Variables must be declared
• Functions defined only at the top scope level
• “this” is undefined at the global level
• Throws error when you forget the 'new' keyword in constructor
• Can't use the same function parameter twice
![Page 44: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/44.jpg)
Array Functions• .isArray()
• .every()
• .forEach()
• .indexOf()
• .lastIndexOf()
• .some()
• .map()
• .reduce()
• .filter()
![Page 45: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/45.jpg)
forEachlet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // forEach iterates over the array, once for each element, but there is no way to // break outnums.forEach(function (elem, index, arr) { console.log(index + ': ' + elem); });
![Page 46: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/46.jpg)
maplet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // map iterates over all of the elements and returns a new array with the same // number of elementslet nums2 = nums.map((elem) => elem * 2); console.log(nums2);
![Page 47: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/47.jpg)
filterlet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // filter iterates over the array and returns a new array with only the elements // that pass the testlet nums3 = nums.filter((elem) => !!(elem % 2)); console.log(nums3);
![Page 48: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/48.jpg)
reducelet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // reduce iterates over the array passing the previous value and the current// element it is up to you what the reduction does, let's concatenate the stringslet letters2 = letters.reduce((previous, current) => previous + current); console.log(letters2); // reduceRight does the same but goes from right to leftlet letters3 = letters.reduceRight((previous, current) => previous + current); console.log(letters3);
![Page 49: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/49.jpg)
everylet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // every makes sure that all the elements match the expressionlet isEveryNumbers = junk.every((elem) => typeof elem === 'number'); console.log('Are all members of junk numbers: ' + isEveryNumbers);
![Page 50: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/50.jpg)
JSON Object• JSON strings must be double quote
• key/value pairs
• key is any valid JS string
• value is an valid JS data type
• .stringify() - converts object to JSON string
• .parse() - converts JSON string to object
![Page 51: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/51.jpg)
JSON data{ "title": "U.S. Citizenship", "tagLine": "Think you can pass this quiz of basic knowledge of American government, history, and geography?", "added": "2015-07-04T18:25:43.511Z", "rating": 3, "_id": 1, "tags": [ "history", "geography", "United States" ] }
![Page 52: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/52.jpg)
How do you turn a JS object into a JSON string?
A. JSON.parse()
B. Math.obj()
C. .toString()
D. JSON.stringify()
![Page 53: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/53.jpg)
ES6 (ES2015)
![Page 54: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/54.jpg)
ES2015 aka ES6• let
• const
• Template strings
• Arrow functions
• for-of
• Classes
• Promises
• Using ES6 today
![Page 55: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/55.jpg)
let
• let allows us to create a block scoped variables
• they live and die within their curly braces
• best practice is to use let instead of var
![Page 56: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/56.jpg)
let// let allows us to create block scoped variables// they live and die within the curly braceslet val = 2; console.info(`val = ${val}`); { let val = 59; console.info(`val = ${val}`); } console.info(`val = ${val}`);
![Page 57: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/57.jpg)
const
• const creates a variable that can't be changed
• best practice is to make any variable that should not change a constant
• does not apply to object properties or array elements
![Page 58: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/58.jpg)
constconst name = 'Troy'; console.info(`My name is ${name}`); // the line below triggers a type errorname = 'Miles';
![Page 59: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/59.jpg)
Template strings
• Defined by using opening & closing back ticks
• Templates defined by ${JavaScript value}
• The value can be any simple JavaScript expression
• Allows multi-line strings (return is pass thru)
![Page 60: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/60.jpg)
Template strings let state = 'California'; let city = 'Long Beach'; console.info(`This weekend's workshop is in ${city}, ${state}.`); // template strings can run simple expressions like addition let cup_coffee = 4.5; let cup_tea = 2.5; console.info(`coffee: $${cup_coffee} + tea: $${cup_tea} = $${cup_coffee + cup_tea}.`); // they can allow us to create multi-line strings console.info(`This is line #1.this is line #2.`);
![Page 61: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/61.jpg)
Arrow (lambda) functionlet anon_func = function (num1, num2) { return num1 + num2; }; console.info(`Anonymous func: ${anon_func(1, 2)}`); let arrow_func = (num1, num2) => num1 + num2; console.info(`Arrow func: ${arrow_func(3, 4)}`);
![Page 62: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/62.jpg)
In a loop what do the break and continue instructions do?
A. break cause the program to halt all execute and continue resumes execution
B. break goes immediately to the top of the loop and continue exits the loop
C. break exits the loop and continue goes immediately to the top of the loop
![Page 63: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/63.jpg)
for-of• for of lets us iterate over an array
• .entries() gives us the index and the element
• with destructuring we can use as variables
• unlike forEach, continue and break work
• break stop the loop
• continue stops the current iteration
![Page 64: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/64.jpg)
for (let elem of pirateArray) { console.info(elem); } // if we want the element and its index we can get it toofor (let [index, elem] of pirateArray.entries()) { if (index === 5) { break; } console.info(`${index}. ${elem}`); } // unlike forEach, continue and break workfor (let [index, elem] of pirateArray.entries()) { if (index === 5) { continue; } console.info(`${index}. ${elem}`); }
for-of
![Page 65: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/65.jpg)
class// class must exist before usageclass Animal { constructor(name) { this.name = name; this.legs = 2; } showNameAndLegs() { console.info(`${this.name} has ${this.legs} legs.`); } } var myBird = new Animal("Bird"); myBird.showNameAndLegs();
![Page 66: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/66.jpg)
Promisefunction sometimesWorks() { // note: all this function does is return a promise // the promise returns immediately to the caller return new Promise( function (resolve, reject) { // number of milliseconds to delay let delay = getRandomNum(1000, 3000); setTimeout(function(){ // whether it is successful or not let worked = !!Math.floor(getRandomNum(0, 2)); if(worked){ resolve('It worked!'); } else { reject('It failed'); } }, delay); }); }
![Page 67: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/67.jpg)
PromisesometimesWorks().then( // if it is successful, it will come here function (data) { console.info(data); }, // if it fails it will come here function (err) { console.info(err); } ); console.info('Back from promise land');
![Page 68: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/68.jpg)
What is the difference between var and let?
A. var is scoped to blocks and let is scoped to functions
B. let allows you to create a constant, var is for variables
C. let is scoped to blocks and var is scoped to functions
D. Nothing, let is the same as var
![Page 69: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/69.jpg)
Default parameters
• Automatically assign a value to undefined parameters
• Always taken from left to right
![Page 70: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/70.jpg)
Default parameter example
1 var add = (x=1, y=2) => x + y; 2 console.info(add(10));
![Page 71: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/71.jpg)
ES2016 aka ES7
• array.prototype.includes
• Exponentiation operator **
![Page 72: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/72.jpg)
How to use modern JS now?
• Use only the latest browsers
• Use a transpiler: Babel, Traceur, Closure, TypeScript
• Use Node.js
• https://kangax.github.io/compat-table/es6/
![Page 73: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/73.jpg)
TypeScript
![Page 74: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/74.jpg)
JavaScript Problems• No modules, only files
• No linkage
• No static checking
• Hard to refactor
• So Google created AtScript
![Page 75: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/75.jpg)
The death of AtScript• Google forked TypeScript,
• Called it AtScript for @ signed used annotations
• Google and Microsoft engineers met
• Microsoft added some desired functionality to TypeScript
• TypeScript became the preferred language of Angular 2
![Page 76: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/76.jpg)
TypeScript to JavaScript
• Use system.js to compile in browser
• Use typescript command
• Use npm/system.js/typescript to auto-compile
![Page 77: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/77.jpg)
Types• Boolean
• Number
• String
• Array
• Tuple
• Enum
• Any
• Void
![Page 78: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/78.jpg)
Interfaces• Functions
• Properties
• Arrays
• Extends other interfaces
• How to choose interface or class?
![Page 79: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/79.jpg)
Decorators
• A proposed standard for ES2016
• Created by Yehuda Katz (Ember)
• Implemented in TypeScript
• Implement Angular’s Annotation
![Page 80: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/80.jpg)
Annotation• A declarative way to add metadata to an object
• Three types:
• Type
• Field
• Class
![Page 81: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/81.jpg)
Annotation Exampleimport {Component, OnInit} from 'angular2/core'; import {QuizService} from './quiz-service'; import {ROUTER_DIRECTIVES} from 'angular2/router'; @Component({ selector: 'quiz', templateUrl: './templates/quiz.html', directives: [ROUTER_DIRECTIVES], providers: [QuizService]}) export class QuizComponent implements OnInit { quizList:IQuizList[] = []; constructor(private _quizService:QuizService){ } ngOnInit() { this.getQuiz(); } getQuiz() { this._quizService.getQuizzes().then((quiz) => { this.quizList = quiz }, (error)=>console.log(error)); } }
![Page 82: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/82.jpg)
Modules
• Any file containing a top-level import or export is considered a module
• Modules are executed within their own scope
• Modules are declarative
• Modules import one another using a module loader
![Page 83: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/83.jpg)
tsconfig.json{ "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules", "typings/main", "typings/main.d.ts" ] }
![Page 84: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/84.jpg)
tsconfig.json
• target: compile to this version of JS
• module: module system to use
• moduleResolution: where modules are loaded
• sourceMap: should a source map be generated
![Page 85: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/85.jpg)
tsconfig.json• emitDecoratorMetadata: set true to include the
decorator metadata, required for Angular 2
• experimentalDecorators: should we use ES7 style decorators
• removeComments: should comments be deleted
• noImplicitAny: set true if you want super strict typing, (you probably don’t want this)
![Page 86: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/86.jpg)
• npm install -g typescript
• git clone https://github.com/Rockncoder/quizzer-start.git
• cd quizzer-start
• npm install
• npm start
![Page 87: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/87.jpg)
Angular 2
![Page 88: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/88.jpg)
Bootstrapping
• Angular apps built with components
• One component is chosen by the developer to kick off the app
• This process is known as bootstrapping
![Page 89: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/89.jpg)
Main Parts• Metadata
• Component
• Template/View
• Data Binding
• Module
• Service
• Directive
• Dependency Injection
![Page 90: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/90.jpg)
Metadata
• Metadata is extra information which gives angular more info
• @Component tells angular the class is a component
• @Directive tells angular the class is a directive
![Page 91: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/91.jpg)
Component• A class with component metadata
• Responsible for a piece of the screen referred to as view.
• Template is a form HTML that tells angular how to render the component.
• Metadata tells Angular how to process a class
![Page 92: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/92.jpg)
Componentimport {Component, OnInit} from 'angular2/core'import {QuizService} from './quiz-service'@Component({ selector: 'quiz', templateUrl: './templates/quiz.html', providers: [QuizService]}) export class QuizComponent implements OnInit { quizList: IQuizList[]; constructor(private _quizService:QuizService) { } ngOnInit() { this.getQuiz(); } getQuiz() { this.quizList = this._quizService.getQuizzes(); } }
![Page 93: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/93.jpg)
Template/View• Is a way to describe a view using HTML
• Templates can be included with the component
• Or as an URL link to an HTML file
• Best practice is to use an HTML file
• The moduleId: module.id property sets the base for module-relative loading of the templateUrl.
![Page 94: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/94.jpg)
moduleId
• The moduleId: module.id
• Sets the base for module-relative loading of the templateUrl.
![Page 95: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/95.jpg)
Service
• “Substitutable objects that are wired together using dependency injection (DI)”
• Used to share code across app
• Lazily instantiated
![Page 96: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/96.jpg)
Directives
![Page 97: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/97.jpg)
Directive• A class with directive metadata
• Two kinds: attribute & structural
• Attribute directives alter the look or behavior of an existing element
• Structural directives alter the layout by adding, removing, and replacing elements in the DOM
• All structural directives begin with “*”
• A component is a directive with a view
![Page 98: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/98.jpg)
Directives
Directive Purpose
*ngIf adds or removes part of the DOM
*ngFor implements a template
*ngSwitchCase teams up with ngSwitch & *ngSwitchDefault
![Page 99: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/99.jpg)
Custom Directiveimport {Directive, ElementRef, Renderer, Input, OnInit} from 'angular2/core'; @Directive({ selector: '[sizer]'}) export class Sizer implements OnInit { @Input() sizer:string; element:ELementRef; renderer:Renderer; constructor(element:ElementRef, renderer:Renderer) { this.element = element; this.renderer = renderer; } ngOnInit() { this.renderer.setElementStyle(this.element.nativeElement, 'fontSize', this.sizer + '%'); } }
![Page 100: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/100.jpg)
Component + Directiveimport {Directive, Component, ElementRef, Renderer} from ‘@angular/core'; import {Sizer} from './sizer'@Component({ selector: 'my-app', providers: [], template: ` <div> <p [sizer]="200">Butter{{name}}</p> </div> `, directives: [Sizer]}) export class App { constructor() { this.name = 'Monkey' } }
![Page 101: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/101.jpg)
Dependency Injection• A way to supply a new instance of a class with the
fully-formed dependencies it needs
• Most dependencies are services
• Angular know which services a components by looking at the types of its constructor parameters
• Services are injected by an Injector which uses a Provider to create the service
![Page 102: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/102.jpg)
Module
• Modules are optional but a best practice
• export tells TypeScript that the resource is a module available for other modules
• import tells TypeScript the resource in a module
• Angular ships a collection library modules
![Page 103: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/103.jpg)
What is the difference between a component and a directive?
• Nothing, they are the same thing.
• A component is a directive with a view.
• A directive is a decorator and a component is an attribute.
• All of the above.
![Page 104: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/104.jpg)
Lifecycle Hooks• ngOnChanges
• ngOnInit
• ngDoCheck
• ngAfterContentInit
• ngAfterContentCheck
• ngAfterViewInit
• ngAfterViewChecked
• ngOnDestroy
![Page 105: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/105.jpg)
Forms
![Page 106: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/106.jpg)
Forms in Angular
• Template-Driven Forms
• Model-Driven Forms
![Page 107: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/107.jpg)
Data Binding
C/D Attribute Binding type
—> {{ value }} one-way
—> [property] = “value” property
<— (event) = “handler” event
<—> [(ng-model)] = “property” two-way
![Page 108: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/108.jpg)
Using Forms
• Forms module must imported
• Forms use two-way binding via [(ngModel)]
• name attribute is required on form tags
![Page 109: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/109.jpg)
NgForm
• Added to the form element automatically
• Holds the forms controls and their validity state
• Also has it own validity
![Page 110: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/110.jpg)
ngModel Change-StateControl State If true If false
Visited ng-touched ng-untouched
Changed ng-dirty ng-pristine
Valid ng-valid ng-invalid
![Page 111: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/111.jpg)
Control Classes
• *.pristine - true if the user has not touched the control
• *.touched - true if the user has left the control
• *.valid - true if the control is valid
![Page 112: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/112.jpg)
template reference variable
• #<name>=“ngModel” (template ref. variable)
• ngModel is required here
• creates a variable named, <name>
• (it relates to the directive’s exportAs property)
![Page 113: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/113.jpg)
t.r.v. properties
• .value
• .valid / .invalid
• .pristine / .dirty
• .touched / .untouched
![Page 114: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/114.jpg)
Firebase
![Page 115: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/115.jpg)
Installing Firebase
• npm i -S firebase angularfire2
• Add to app module and initialize
• Import into code
• Using firebase is different than hosting on it
![Page 116: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/116.jpg)
Firebase settings
• Go to the Firebase console
• Click on Overview
• Click on “Add Firebase to your web app”
• Copy your config settings
![Page 117: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/117.jpg)
Configure Firebase8 import {AngularFireModule} from "angularfire2"; 9 10 export const firebaseConfig = { 11 apiKey: "AxAxAxAxAxAxAxAxAxAxAxAxAxAxAxAxAxAxAxAx", 12 authDomain: “an-app-42.firebaseapp.com”, 13 databaseURL: "https:#//an-app-42.firebaseio.com", 14 storageBucket: "an-app-42.appspot.com", 15 messagingSenderId: "xxxxxxxxxxxx" 16 }; 17 18 @NgModule({ 19 imports: [ 20 BrowserModule, 21 FormsModule, 22 HttpModule, 23 AngularFireModule.initializeApp(firebaseConfig) 24 ], 25 declarations: [ 26 AppComponent 27 ], 28 providers: [], 29 bootstrap: [AppComponent] 30 })
![Page 118: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/118.jpg)
RxJS
![Page 119: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/119.jpg)
Reactive Extensions
• A library for building async and event-based programs using observable data streams
• Data streams can be from a file, web service, or user input
![Page 120: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/120.jpg)
Use Rx for…• UI events like mouse move, button click
• Domain events like property changed, collection updated, etc.
• Infrastructure events like from file watcher, system and WMI events
• Integration events like a broadcast from a message bus or a push event from WebSockets API
![Page 121: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/121.jpg)
Observable
• Emit the next element
• Throw an error
• Send a signal that streaming is finished
![Page 122: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/122.jpg)
Observer
• Handle the next emitted element
• Handle errors
• Handle the end of streaming
![Page 123: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/123.jpg)
![Page 124: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/124.jpg)
Jasmine
![Page 125: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/125.jpg)
Jasmine• Created by Pivotal Labs in 2010
• Current version 2.5.3
• JavaScript testing framework
• The default unit test for Angular
• Can test client and server code
![Page 126: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/126.jpg)
describe() - Test Suite• describe() is a global Jasmine function
• Takes to parameters
• name of the test suite (string)
• implementation of the suite (function)
• Can be nested
![Page 127: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/127.jpg)
describe()describe('App: Quizzer', () => { beforeEach(() => { TestBed.configureTestingModule({ declarations: [ AppComponent ], imports: [ RouterTestingModule ] }); }); });
![Page 128: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/128.jpg)
it() - Test Spec
• it() is a global Jasmine function
• Takes two parameters
• name of the spec (string)
• implementation of the spec (function)
![Page 129: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/129.jpg)
it()
it(`should have as title 'Quizzer'`, async(() => { let fixture = TestBed.createComponent(AppComponent); let app = fixture.debugElement.componentInstance; expect(app.title).toEqual('Quizzer'); }));
![Page 130: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/130.jpg)
expect() - Expectation• expect() is a global Jasmine function
• Jasmine’s version of assert()
• Takes one parameter
• The actual - value generated by code under test
• Is chained to a Matcher
![Page 131: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/131.jpg)
Matcher
• Takes the output of the expect() function
• Takes one parameter
• The expected value
• Compares the expect and actual value using the logic of the matcher
![Page 132: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/132.jpg)
expect()
let app = fixture.debugElement.componentInstance; expect(app).toBeTruthy(); expect(app).not.toBeUndefined(); expect(app.title).toEqual('Quizzer');
![Page 133: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/133.jpg)
Matchers (part 1)Matcher Comparison
toBe() compares using ===
toEqual() works for literal variables and objects
toMatch() for regular expressions
toBeDefined() compares against 'undefined'
toBeUndefined() also compares against ‘undefined'
![Page 134: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/134.jpg)
Matchers (part 2)Matcher Comparison
toBeNull() compares against null
toBeTruthy() truthy boolean casting
toBeFalsy() falsy boolean casting
toContain() finds an item in array
![Page 135: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/135.jpg)
Matchers (part 3)Matcher Comparison
toBeLessThan() mathematical comparison
toBeGreaterThan() mathematical comparison
toBeCloseTo() precision math comparison
toThrow() should throw an exception
![Page 136: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/136.jpg)
Angular’s MatchersMatcher Comparison
toBePromise() the value is a promise
toBeAnInstanceOf() an instance of an object
toHaveText() the element has the given text
toHaveCssClass() the element has the given CSS class
toHaveCssStyle() the element has the given CSS styles
toImplement() the class implements the given interface
![Page 137: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/137.jpg)
Custom Matchersvar customMatchers = { toBeGoofy: function (util, customEqualityTesters) { return { compare: function (actual, expected) { if (expected === undefined) { expected = ''; } var result = {}; result.pass = util.equals(actual.hyuk, "gawrsh" + expected, customEqualityTesters); result.message = result.pass ? "Expected " + actual + " not to be quite so goofy" : "Expected " + actual + " to be goofy, but it was not very goofy"; return result; } }; } };
![Page 138: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/138.jpg)
beforeEach()
• Setup function
• Called before each spec is executed
• A good place to add customer matchers
![Page 139: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/139.jpg)
beforeEach()
beforeEach(function() { jasmine.addMatchers(customMatchers); });
![Page 140: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/140.jpg)
this
• beforeEach sets the this construct to any empty object
• It is passed to each it() and afterEach()
• The modified this doesn’t flow thru from one it() to the next
![Page 141: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/141.jpg)
afterEach()
• Teardown function
• Called after each spec is executed
![Page 142: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/142.jpg)
Disabling suites & specs
• prepend an ‘x’
• to disable a suite change describe() to xdescribe()
• to disable a spec change it() to xit()
• They are not execute but appear in reporting
![Page 143: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/143.jpg)
Dependency Injection is not Magic
• Dependency Injection (DI) is a core component of Angular
• It only means objects are instantiated somewhere else
• And supplied to your code when it needs them
• Angular calls DI a provider
![Page 144: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/144.jpg)
providers@NgModule({ imports: TroyModules, declarations: [ AppComponent, AboutComponent, LoginComponent, QuizComponent, PlayerComponent, MixedPipe, BackcolorDirective, ModelComponent ], providers: [QuizService], bootstrap: [AppComponent] }) export class AppModule { }
![Page 145: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/145.jpg)
Module InitializationKey Value
imports Modules (array)
declarations Components (array)
providers Services and other DI objects (array)
bootstrap the launch component
![Page 146: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/146.jpg)
Unit Test & DI
• When unit testing your app you ask Angular for just enough parts to make it work
• Angular uses DI to do this
![Page 147: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/147.jpg)
Unit Tests & Providersdescribe('Router', () => { beforeEach(() => { // acts like NgModule TestBed.configureTestingModule({ imports: [ RouterTestingModule, AppRoutingModule ], declarations: [ AppComponent, AboutComponent, LoginComponent ], providers: [ {provide: APP_BASE_HREF, useValue: '/'} ] }); });
![Page 148: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/148.jpg)
Links
• https://angular.io/
• https://material.angular.io/
• https://github.com/systemjs/systemjs
![Page 149: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/149.jpg)
• https://github.com/angular/angularfire2
• http://blog.angular-university.io/angular-2-firebase/
![Page 150: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/150.jpg)
Links• http://www.sitepoint.com/angular-2-mockbackend/
• https://auth0.com/blog/2015/09/03/angular2-series-working-with-pipes/
• https://developers.livechatinc.com/blog/testing-angular-2-apps-routeroutlet-and-http/
• https://medium.com/google-developer-experts/angular-2-introduction-to-new-http-module-1278499db2a0#.xey81v6eq
![Page 151: Angular 2 Weekend Light](https://reader036.vdocument.in/reader036/viewer/2022062316/58e49e361a28abf5428b5915/html5/thumbnails/151.jpg)
Summary
• Angular 2 is simpler than AngularJS
• TypeScript is the preferred language for Angular 2
• The framework is made to be easily testable