Introduction
In this tutorial we’re going to learn the Javascript Framework Angular
We will see together the base elements such as Component, Module, Service and Directives.
Anatomy of an Angular Application
A component is the combination of logic (class TypeScript) and views (the template)
Component
Component is every where in Angular.
Let’s have a closer look.
import { Component, ViewEncapsulation } from '@angular/core';
@Component{
selector: 'book',
templateUrl: 'book.component.html',
styleUrls: ['book.component.css'],
encapsulation: ViewEncapsulation.Emulated
})
export class BookComponent {
...We’ll look first at Metadata applied to the Component :
Metadata is nothing more than properties inside @Component annotation.
* selector : It is just a name, coz a component can be instanciated many times in many views, so it need to have a name: Each time Angular finds an element in our HTML that matches the selector, Angular create an instance of that component.
* templateUrl : indicates the path of the HTML template.
You can, however, have an template and directly write the DOM* styleUrls : takes an array of URLs for CSS style-sheets.
Other way to do this is to set styles that contain CSS code.
- CSS encapsulation will be done by Angular ViewEncapsulation.Emulated (default, Angular will accept: ViewEncapsulation.None OR ViewEncapsulation.Native
Module
Our application will always have at least one module, the root module @NgModule.
One or more @NgModule annotation match a specific Component (or group of Component) to a block of code that handles it.
Here’s how it looks in pseudocode:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { WelcomeComponent } from './home/welcome.component';
import { BookService } from './shared/book.service';
@NgModule({
imports: [ BrowserModule
// Some Others Modules
],
declarations: [ AppComponent,
WelcomeComponent,
// Some Others Component
],
providers: [BookService],
// Some Others providers
bootstrap: [ AppComponent ]
})
export class AppModule { }
/* export Some others Modules
and exclude the bootstrap
to recognize just who is the root module
*/As this module is the root module, we must tell Angular which component is the root component : This is the objective of the bootstrap field.
In this pseudocode example we find other sections then bootstrap, what each section mean:
imports: (only module classes are accepted), just like BrowserModule. We are building an application for the browser.
declarations: Updating this array when you add a new component (ng generate component with CLI does it automatically)
providers: Resources that can be injected (DI) in a component (DI = injection by constructor)
Surely, the compiler can find the problem, when there are no references to it, anything missing… :)
Databinding & Component
Databinding means you’re passing data between the component class and the view.
There are 4 ways to define a Databinding in Angular:
1. Interpolation
Interpolation is a one-way data-binding. You can use interpolation {{}} to define the value which is used to output the data from a TypeScript code to HTML template:
let stringInterpolation: string = 'This is string interpolation!';
let numberInterpolation: number = 2;{{stringInterpolation}}
{{numberInterpolation}}2. Property Binding
Property binding is also one way : the data is transferred from the component to the class. We bind a proprty of the DOM element, Just like src, the attribute of the img element.
There are 3 ways to define a property binding in Angular:
1. You can use interpolation
<img src="{{logo}}">2. You can define property binding by enclosing brackets around an element property.
<img [src]="logo">3. The same thing by adding bind- before the element property.
<img bind-src="logo">For property binding, This is another common use-case is an input element disabled attribute.
<button [disabled]="status">Show book</button>3. Event Binding
When a user interacts with an application in the form of a keyboard movement, a mouse click, or a mouseover, it generates an event. These events need to be handled to perform some kind of action.
Let us consider an example to understand this better.
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'event-binding',
template: `Click me!`,
styles: []
})
export class EventBindingComponent {
@Output() clicked = new EventEmitter<string>();
onClicked() {
this.clicked.emit('It works!');
}
}In Other Component we can inject this : event-binding. to handle this kind of event.
<event-binding (clicked)="onClicked($event)"></event-binding>onClicked(value: string) {
......
}4. Two-Way Binding
Two-Way Binding means that:1. When any model data changes, so immediately the values of this model are propagated to the matching view(s)
2. When the view elements get updated, the changes get propagated back to the model.
As you can see, combining these both Events and Preperty Binding, we could create a two-way binding property in components. The syntax would be [( )].
Services and Dependency Injection
Dependency Injection:
In general, there are many ways an element can get a control of its dependencies.In Angular project, a Component can ask Angular directly about dependencies with the other Modules. TypeScript compiler does all the work .
For more information about Typescript, Compiler and Transpiler please visit the web site of the software :
TypeScript
ECMASCRIPT 6
Service
Get Samir Romdhani’s stories in your inbox
Join Medium for free to get updates from this writer.
The key point to remember about a Services, those who having annotation @Injectable and they will be provided as a singleton when they are included in the array of providers in @NgModule class, we don’t care if it’s a root module or not.
Let’s look at an example:
@Injectable()
export class BookService {
....import { Component } from '@angular/core';
import { BookService } from './book.service';
@Component({
selector: 'book',
templateUrl: 'book.component.html',
styleUrls: ['book.component.css']
})
export class BookComponent {
constructor(private bookService: BookService) {
}@NgModule({
providers: []
// We must updating this array
})
export class AppModule { }The code won’t compile: We have references to BookService.
The compiler is happy to show us the problem.
Since the upgrade to the Angular 6 providers array was like a small surgical strike. In our module it seems empty, to be not working.
mport { NgModule } from '@angular/core';
@NgModule({
providers: [],
// NO Some Others providers
})
export class AppModule { }So, then, what is happening when this array seems to be changing?
Let’s find out.
import { Injectable } from '@angular/core';
@Injectable( {
providedIn: 'root',
} )
export class BookService {
....It’s not required that The providedIn: ‘root’ solution be in all element @Injectable, we can simply inject needed services and we’re happy.
Directives
In this section, we’re going to do a really basic introduction to Directives.
We’re starting from the very beginning to MANIPULATE ALL THE DOM !!!
As you’ve seen ROWSPAN & COLSPAN are attributes of the two elements of table, th and td .
Manipule the DOM is easy with Angular, You can modify an element declaration using the keyword *ngFor, *ngIf and others, also we can take a Property Binding of existing attributes as we have seen earlier.
For More about existing directive, please visit Angular web site.
Of course, the cool thing here is that any element in the DOM from any Component can also implement its own directive.
How do you know for sure whether a directive will be created? Because you didn’t write any class with annotation @Directive in your application.
The following is an example of read directive:
import { Directive, ElementRef, Input} from '@angular/core';
@Directive({
selector: '[read]'
})
export class ReadDirective {
// set a constructor for class here. # line 6
// set a constructor for [read] here. # line 7
...* * * start to finish * * * *
In this pseudocode example, lines 6 means that you shoud know the source element.
constructor( elementRef: ElementRef) {}And lines 7 means you should get a value from source element and do something.
@Input() set read( value: any) { ... }Than In book.component.html we can inject this
<h2 [read]="{{book.author}}"></h2>Finish this point by updating the array of declarations in the root module.
…






