what's new in angular 2?
TRANSCRIPT
What’s new in Angular 2?A COMPARISON BETWEEN NG1.X AND NG2
Alfred Jett GrandezaFounder, Leangineer @ Lean Consulting
leanconsulting.ph | ajgrandeza.com
Lean Consulting• Davao based start-up• Founded: February 2016• Lean Academy SOON!• More info:
• leanconsulting.ph• fb.com/leanconsutling.ph
About Me•Graduate of Ateneo de Davao, Computer Science 2012
•Over 5 years of software engineering experience, .NET and AngularJS
•Former CTO of T.H.E. Patrons
•Co-founder of Lean Consulting/Lean Academy
•Microsoft Technology Associate (2)
•Member of Davao .NET Community (Davao .NET Users Group, Philippine .NET Users Group)
•I do consulting and freelance work from clients overseas
•Advocate of .NET Technologies
•I LOVE TO LEARN!
Prerequisites & Tools• Required• JavaScript• HTML• CSS
• Helpful• OOP concepts
• Visual Studio Community• https://www.visualstudio.com/
• Visual Studio Code• https://code.visualstudio.com/
Single Page Application• SPAs are web apps that load a single HTML page and dynamically that page as the user interacts
• Much of the work happen in the client side
• Usually written in JavaScript
SPA Lifecycle
Angular is…• Open source, JavaScript based front-end framework
• Used for building client-side applications
• MVW Framework (Model-View-Whatever)
• Mainly maintained by Google
Why use Angular?• Expressive/Customizable HTML
• Powerful Data Binding
• Modular
• Easy back-end integration
Application Demo• NG 1.X App
• NG 2 App
Key Features of Angular 1.X• Directives
• Data binding
• Views
• Controllers
• $scope
• Modules
• Dependency Injection
• Routes
• Services
What is a directive?• Extends HTML attributes, with the prefix “ng-”
• Teaches HTML new tricks
• There are built-in directives• ng-app• ng-view
• You can also create your own custom directives
Data Binding• Interpolation• {{name}}
• Event Binding• ng-click=“onClick()”
• Two-way Binding• <input ng-model=“name” />
Filters & Pipes• Filters can be added to filter an array
•Pipes can be used to format a data
Views, Controllers, Scope•View – UI / Display
•Controllers – Controls the logic, data and flow
•$scope is the “glue” between controller and view or the ViewModel
Modules & DI• A container for the different parts of your Angular app
• You can package code as reusable modules
• You may inject different modules to your module• Dependency Injection
• Instantiate a module• ng-app=“”
• Creating a module• angular.module(‘demoApp’, [])
Routes• is used for deep-linking URLs to controllers and views
Services• Are for organizing and share code across your application
Summary for NG1 Key Features• Directives• Data binding• Views• Controllers• $scope• Modules• Dependency Injection• Routes• Services
Angular 2.0 with TypeScript
Live release: Sept 16, 2016
http://angularjs.blogspot.com/2016/09/angular2-final.html
Why Angular 2?• Built for speed
• Modern
• Simplified API
• Enhanced Productivity
• Develop Across All Platforms
What is TypeScript?• Open source programming language
• Developed and maintained by Microsoft
• A superset of JavaScript
• Transpiles to plain JavaScript
• Strongly typed
• Class based / OOP approach
• Learn more:• http://www.typescriptlang.org/Playground
Setting up• Download• https://github.com/angular/quickstart
Angular 2 Key Features• Components
• Templates
• Directives
• Data Binding & Pipes
• Life-cycle hooks
• Services
• Dependency Injection
• Routes
Anatomy of NG 2
Image grabbed from: https://www.pluralsight.com/courses/angular-2-getting-started
Anatomy of a Component
Image grabbed from: https://www.pluralsight.com/courses/angular-2-getting-started
Components
Image grabbed from: https://www.pluralsight.com/courses/angular-2-getting-started
ComponentsImport statements
Metadata & Template
Class
Decorator• A function that adds metadata to a class
• Prefixed with an @
• @Component()
Defining a Template in a Component• Inline template
•Linked Template
Data Binding• Coordinates communication between the component’s class and template
• Interpolation• {{name}}
• Event Binding• (click)=‘onClick()’
• Two-way Binding• <input [(ngModel)]=‘name’ />
• Property Binding• <img [src] = ‘customer.thumbnail’ />
Directive in NG2• Built-in directives• *ngIf• *ngFor
Pipes• | lowercase
• | currency
• | uppercase
• No more filter • You have to create your own pipe
Lifecycle Hooks• A component has a lifecycle
• Angular offers component lifecycle hooks that give us ability to capture these moments
• In other words, events
• Samples• OnInit• OnChanges• OnDestory
Services• Services are independent from any components
• To provide shared data, logic across multiple components
• Encapsulate external interactions (i.e. Rest API)
Services
Dependency Injection• A design pattern to resolve dependencies
Retrieve Data using HttpReturns Observables instead of Promises
What is RxJS?• RxJS is a javascript library for composing asynchronous & event-based programs by using observable sequences
• Used within Angular
• More info• http://reactivex.io/rxjs/
Promises vs Observable• Promise• Returns a single value• Not cancellable
• Observable• Works with multiple values over time• Cancellable• “Retryable”• Supports map, filter, reduce, etc..
Routing
Summary• Huge difference between NG1 & NG2, in terms of syntax
• NG2 is simpler in terms of Architecture
• TypeScript makes the job easier
References•https://angular.io/docs/ts/latest/quickstart.html
•https://www.pluralsight.com/courses/angular-2-getting-started
•https://auth0.com/blog/angular-2-series-part-2-domain-models-and-dependency-injection/
•https://auth0.com/blog/angular-2-series-part-3-using-http/