denis@newventuresoftware.com | |
github.com/deniskyashif | |
@deniskyashif |
Say NO to the slow and painful
git clone https://github.com/newventuresoftware/angular-demos.git
Microsoft
.static typing
.C#
.intellisense
.JavaScript
libraries.It's always better to catch errors at compile time rather that at runtime.
static typing
, it's more predictable.modules, namespaces and stronger OOP
, it scales better for larger apps.compilation step
, some errors are caught compile-time, not run-time.Using the Node Package Manager.
npm install --global typescript
TypeScript is written in .ts files, which can't be used directly in the browser.
It need to be translated to vanilla .js first.
tsc main.ts
Specifies the way TS is compiled.
(autogeneratable with tsc --init)
{
"compilerOptions": {
"target": "es5", // Sets the output JS's version
"module": "commonjs", // Sets the module loader
"outDir": "dist", // Sets output JS files' location
"sourceMap": true, // Allows debugging
"noEmitOnError": true // Do not compile if errors
}
}
“Strongly typed languages reduce bugs by 15%.”
Class<T>
import / export
Every time a new JS Framework comes up...
...but not this time.
A developer platform for building mobile and desktop web apps using TypeScript/JavaScript and other languages.
Consists of several libraries, some of them core and some optional.
With all the front-end build tools, setting up a project can be tedious.
Angular CLI solves this problem!
Setting up a project with the Angular CLI
npm install -g @angular/cli
ng new movies
cd movies
ng serve
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
@Component()
Shadow DOM
is just normal DOM with two differences: Shadow DOM
is designed as a tool for building component-based apps
DOM
tree encapsulation problemDOM
logic behind other elementsIsolated DOM
- A component's DOM is self-contained document.querySelector()
won't return nodes in the component's shadow DOMScoped CSS
- CSS defined inside shadow DOM is scoped to itNone
- no Shadow DOM and no encapsulationEmulated
(default) - no Shadow DOM but there is encapsulation of the viewsNative
- native Shadow DOM{{}} () [] [()]
A mechanism for coordinating the view with the application data.
Creating Angular Components
@Pipe()
@Directive()
|
Directives aim to extends the static nature of HTML. They allow creating custom elements, attributes and even control flow inside the HTML.
Working with Directives and Pipes
@Injectable()
A design principle in which a class should receive its dependencies from external sources rather than creating them itself.
class Car {
public engine: Engine;
constructor() {
this.engine = new Engine();
}
}
What is the problem here?
class Car {
public engine: Engine;
constructor(engine: Engine) {
this.engine = engine;
}
}
The class now receives its dependencies in the constructor.
const engine: Engine = new HybridEngine();
const car = new Car(engine);
Angular ships with its own dependency injection framework. This framework can also be used as a standalone module by other applications and frameworks.
@NgModule({
imports: [BrowserModule],
providers: [UserService],
declarations: [App],
bootstrap: [App]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
Services and DI
Angular Routing.
Set of libraries for composing asynchronous and event-based programs using observable sequences
const subscription = source
.filter(quote => quote.price > 30)
.map(quote => quote.price)
.forEach(price => console.log(price));
Cold observables start running upon subscription
Hot observables are ones that are pushing even when you are not subscribed to the observable
It’s not always possible from the subscriber side to know whether you are dealing with a cold or hot Observable
Observables | Promise |
---|---|
Observables handle multiple values over time | Promises are only called once and will return a single value |
Observables are cancellable | Promises are not cancellable |
Observables are lazy | Promises NOT |
// Fetch all existing comments
getComments() : Observable<Comment[]> {
// ...using get request
return this.http.get('/api/comments')
// ...and calling .json() on the response to return data
.map((res:Response) => res.json())
//...errors if any
.catch((error:any) => Observable.throw(error.json().error));
}
service.getComments()
.subscribe(comments => this.comments = comments);
Observables and HTTP.
ngOnInit()
Angular calls lifecycle hook methods on directives and components as it creates, changes, and destroys them.
Constructor | The constructor has been invoked. |
OnChanges | The data-bound input properties have been (re)set. |
OnInit | The component/directive has been initialized. |
DoCheck | Detect and act upon changes that Angular can't or won't detect on its own. |
AfterContentInit | After Angular projects external content into the component's view. |
AfterContentChecked | After Angular checks the content projected into the component. |
AfterViewInit | After Angular initializes the component's views and child views. |
AfterViewChecked | Called just before Angular destroys the directive/component. |
OnDestroy | After Angular checks the component's views and child views. |
* TypeScript, Components & Directives, Component Only
Kendo UI for Angular components are distributed via npm.
npm install --save [kendo-component]
Every component represents a separate Angular module.
Components in Action
Kendo UI is much more that a component suite.
The Data Query provides functions that help you handle the following bulk data operations:
sorting, filtering, grouping, aggragates
Kendo UI for Angular
building for development and production
ng build [--prod]
Publish the contents of the generated dist/
folder to a web server.
ng build --prod
AOT Compilation | Pre-compiles Angular component templates. |
Bundling | Concatenates modules into a single file. |
Inlining | Pulls template html and css into the components. |
Minification | Removes excess whitespace, comments, and optional tokens. |
Uglification | Rewrites code to use short, cryptic variable and function names. |
Dead Code Elimination | Removes unreferenced modules and unused code. |
Prune Libraries | Drop unused libraries and pare others down to the features you need. |
The TypeScript compiler generates source map files, which allow browsers to recover the original source code from the compiled javascript.
>_ ng lint
denis@newventuresoftware.com | |
github.com/deniskyashif | |
@deniskyashif |