Probabilmente il framework javascript più atteso di sempre, evoluzione di uno dei framework più longevi ed usati nello sviluppo front end. Si vedranno alcune delle novità introdotte e delle scelte radicali fatte da Google per la nuova versione di Angular
4. AngularJS history
• AngularJS was originally developed in 2009 by Misko
Hevery and Adam Abrons
• Misko Hevery started to work for Google in 2009
• 1st release of AngularJS: 1 developer, 3 weeks, 1000 loc
• AngularJS version 1.0 was released in 2012 by Google
• Angular version 2 was released in September 2016 after
2 years development
20. AngularJS $digest cycle
• AngularJS engine is built using a dirty checking algorithm.
• Application state is a single entity connected to every
visual component and calculated every time a
component mutates some data
• It’s very easy to trigger unwanted $digest cycles
impacting performances
• Very difficult to debug
21. Angular 2 Change Detection engine
• Based on ngZone
• Recalculate the components tree state after every async
interaction (events, timers, observables..)
• Every component has its own Change Detector
• Component’s Change Detector is generated at runtime
to improve performances
• Developers can control how and when components are
recalculated
22. – ngBook2
“When one of the components change, no
matter where in the tree it is, a change
detection pass is triggered for the whole tree,
from top to bottom.”
23. CD
CD CD
CD CDCD
CD CD
CHANGE DETECTION
TRAVELS TOP TO BOTTOM
ChangeDetection
Flow
25. • Every component gets a change detector responsible
for checking the bindings defined in its template
• ChangeDetectionStrategy
• default: update the component every time data
changes
• onPush: update the component only when its
inputs change or the component requests to be
updated
29. Why typescript
• Angular2 Dependency Injection system is based on
type reflection
• Annotations offer a powerful and very expressive way to
describe elements
30. Pros
• Improve Developer Experience with better tools
• Compile time error check
• Type safety
• Better documentation
• Easy to adopt for backend developers
31. Cons
• Increase project’s technical debt
• Slower learning curve for traditional javascript developer
• Impossible to remove without a complete rewrite
33. Modern web is all about components
• Thinking of components instead of views improves
decoupling and separation of concerns
• Components are composable and highly reusable
• Easier to test
• UX and UI teams integrate better
34. A component is
• exported as a custom HTML tag: <tab-bar />
• defined by an HTML template
• enhanced using the @component decorator
• controlled using its inputs and outputs
• initialized by Angular Dependency Injection engine
39. template is an HTML string that tells Angular what
needs to be to rendered in the DOM.
templateUrl is a relative path to a file containing the
component HTML string.
40. TEMPLATE SYNTAX
• template tags {{ expression }}: Execute arbitrary
expressions. Eg: {{1+1}.
• property binding [key]=“value”. Used to pass data to a
component.
• event binding (event)=“expression”. Expression
executed anytime the registered event fires.
• 2-way binding <input [(ngModel)]=“u.name"> Requires
to import `FormsModule` to be used.
61. • Emulated (default) - styles from main HTML propagate to the
component. Styles defined in this component's @Component
decorator are scoped to this component only.
• Native (shadow DOM)- styles from main HTML do not
propagate to the component. Styles defined in this component's
@Component decorator are scoped to this component only.
• None - styles from the component propagate back to the main
HTML and therefore are visible to all components on the page.
———
Be careful with apps that have None and Native components mixed
in the application. All components with None encapsulation will
have their styles duplicated in all components with Native
encapsulation.
65. –Angular official docs
“Observables open up a continuous channel of
communication in which multiple values of data
can be emitted over time […] Angular 2 uses
observables extensively - you'll see them in the
HTTP service and the event system…”
66. –A. Staltz
“A stream is a sequence of ongoing events
ordered in time. It can emit 3 different things: a
value, an error, or a "completed" signal.
Consider that the "completed" takes place, for
instance, when the current window is closed.”
68. • Both provide us with abstractions that help us deal
with the asynchronous nature of our applications.
• Observables are cancellable.
• Observables can be retried using one of the retry
operators provided by the API, such as retry and
retryWhen.
• Promises require the caller to have access to the
original function that returned the promise in order
to have a retry capability.
72. Bootstrapping is an essential process in Angular - it is
where the application is loaded when Angular comes to
life.
Bootstrapping Angular 2 applications is certainly
different from Angular 1.x, but is still a straightforward
procedure.
73. // app.modules.ts
import { BrowserModule } from '@angular/platform-
browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './[PATH]/app.component';
import { MyComponent } from ‘./[PATH]/some.component';
import { SomeService } from “./[PATH]/some.service";
@NgModule({
declarations: [AppComponent, MyComponent],
providers: [SomeService],
imports: [BrowserModule, HttpModule],
bootstrap: [AppComponent]
})
class AppModule {}
74. // main.ts
import { platformBrowserDynamic } from '@angular/
platform-browser-dynamic';
import { AppModule } from './app/';
// Bootstrap main component
platformBrowserDynamic().bootstrapModule(AppModule);