<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samir Romdhani on Medium]]></title>
        <description><![CDATA[Stories by Samir Romdhani on Medium]]></description>
        <link>https://medium.com/@romdhanisam?source=rss-1a0a66070db8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bfFkkRBkL2VNTp3ZMYADPA.jpeg</url>
            <title>Stories by Samir Romdhani on Medium</title>
            <link>https://medium.com/@romdhanisam?source=rss-1a0a66070db8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 14:28:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@romdhanisam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction]]></title>
            <link>https://medium.com/angular-instance/articles-sur-angular-a55963e8bb?source=rss-1a0a66070db8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a55963e8bb</guid>
            <category><![CDATA[ng]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Samir Romdhani]]></dc:creator>
            <pubDate>Sat, 25 Jan 2020 21:04:04 GMT</pubDate>
            <atom:updated>2022-11-21T01:21:56.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*rMXs8JdAFhIUuHLEdN81kQ.png" /></figure><p>In this tutorial we’re going to learn the Javascript Framework Angular</p><p>We will see together the base elements such as <strong>Component</strong>, <strong>Module</strong>, <strong>Service</strong> and <strong>Directives.</strong></p><p><strong>Anatomy of an Angular Application</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*SwmfOwfmaaZtF3dZ-V97Fw.png" /></figure><p>A component is the combination of logic (class TypeScript) and views (the template)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*x-4f68XbR7fvhWgsKjf9TQ.png" /></figure><p><strong>Component</strong></p><p>Component is every where in Angular.<br>Let’s have a closer look.</p><pre>import { Component, ViewEncapsulation } from &#39;@angular/core&#39;;<br> <br>@Component{<br>  selector: &#39;book&#39;,<br>  templateUrl: &#39;book.component.html&#39;,<br>  styleUrls: [&#39;book.component.css&#39;],<br>  encapsulation: ViewEncapsulation.Emulated<br>})<br>export class BookComponent {<br>...</pre><p>We’ll look first at <strong>Metadata</strong> applied to the Component : <br>Metadata is nothing more than properties inside @Component annotation.</p><blockquote><strong>* selector : </strong>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 <strong>selector</strong>, Angular create an instance of that component.</blockquote><blockquote><strong>* templateUrl</strong> : indicates the path of the HTML template.<br>You can, however, have an template and directly write the DOM</blockquote><blockquote><strong>* styleUrls</strong> : takes an array of URLs for CSS style-sheets.<br>Other way to do this is to set <em>styles</em> that contain CSS code.<br>- CSS encapsulation will be done by Angular <strong>ViewEncapsulation.Emulated (default, </strong>Angular will accept: ViewEncapsulation.None OR ViewEncapsulation.Native</blockquote><p><strong>Module</strong></p><p>Our application will always have at least one module, the root module <em>@NgModule.</em><br>One or more <em>@NgModule</em> annotation match a specific Component (or group of Component) to a block of code that handles it. <br>Here’s how it looks in pseudocode:</p><pre>import { NgModule } from &#39;@angular/core&#39;;<br>import { BrowserModule } from &#39;@angular/platform-browser&#39;;<br>import { AppComponent }  from &#39;./app.component&#39;;<br>import { WelcomeComponent } from &#39;./home/welcome.component&#39;;<br>import { BookService } from &#39;./shared/book.service&#39;;<br> <br>@NgModule({<br>  imports: [ BrowserModule<br>             // Some Others Modules<br>  ],<br>  declarations: [ AppComponent,<br>                  WelcomeComponent,<br>                 // Some Others Component<br>                ],<br>  providers: [BookService],<br>  // Some Others providers<br>  bootstrap: [ AppComponent ]<br>})<br>export class AppModule { }<br>/* export Some others Modules<br>   and exclude the bootstrap<br>   to recognize just who is the root module<br>*/</pre><p>As this module is the root module, we must tell Angular which component is the root component : This is the objective of the <strong>bootstrap</strong> field.</p><blockquote>In this pseudocode example we find other sections then bootstrap, what each section mean:</blockquote><blockquote><strong><em>imports</em></strong>: (only module classes are accepted), just like BrowserModule. We are building an application for the browser.</blockquote><blockquote><strong><em>declarations</em></strong>: Updating this array when you add a new component (ng generate component with CLI does it automatically)</blockquote><blockquote><strong>providers</strong>: Resources that can be injected (DI) in a component (DI = injection by constructor)</blockquote><p>Surely, the compiler can find the problem, when there are no references to it, anything missing… :)</p><blockquote><strong>Databinding &amp; Component</strong></blockquote><blockquote><em>Databinding means you’re passing data between the component class and the view.</em></blockquote><blockquote>There are 4 ways to define a Databinding in Angular:</blockquote><blockquote><strong>1. Interpolation<br></strong>Interpolation is a one-way data-binding. You can use interpolation <strong>{{}}</strong> to define the value which is used to output the data from a TypeScript code to HTML template:</blockquote><pre>let stringInterpolation: string = &#39;This is string interpolation!&#39;;<br>let numberInterpolation: number = 2;</pre><pre>{{stringInterpolation}}<br>{{numberInterpolation}}</pre><blockquote><strong>2. Property Binding<br></strong>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.</blockquote><blockquote>There are 3 ways to define a property binding in Angular:</blockquote><blockquote>1. You can use interpolation</blockquote><pre>&lt;img src=&quot;{{logo}}&quot;&gt;</pre><blockquote>2. You can define property binding by enclosing brackets around an element property.</blockquote><pre>&lt;img [src]=&quot;logo&quot;&gt;</pre><blockquote>3. The same thing by adding <strong>bind-</strong> before the element property.</blockquote><pre>&lt;img bind-src=&quot;logo&quot;&gt;</pre><blockquote><em>For property binding, This is another common use-case is an input element </em><strong><em>disabled</em></strong><em> attribute.</em></blockquote><pre>&lt;button [disabled]=&quot;status&quot;&gt;Show book&lt;/button&gt;</pre><blockquote><strong>3. Event Binding<br></strong>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.<br>Let us consider an example to understand this better.</blockquote><pre>import { Component, EventEmitter, Output } from &#39;@angular/core&#39;;<br> <br>@Component({<br>  selector: &#39;event-binding&#39;,<br>  template: `Click me!`,<br>  styles: []<br>})<br>export class EventBindingComponent {<br>  @Output() clicked = new EventEmitter&lt;string&gt;();<br>  onClicked() {<br>   this.clicked.emit(&#39;It works!&#39;);<br>  }<br>}</pre><blockquote>In Other Component we can inject this : event-binding. to handle this kind of event.</blockquote><pre>&lt;event-binding (clicked)=&quot;onClicked($event)&quot;&gt;&lt;/event-binding&gt;</pre><pre>onClicked(value: string) {<br>  ......<br>}</pre><blockquote><strong>4. Two-Way Binding<br></strong>Two-Way Binding means that:</blockquote><blockquote>1. When any model data changes, so immediately the values of this model are propagated to the matching view(s)</blockquote><blockquote>2. When the view elements get updated, the changes get propagated back to the model.</blockquote><p>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 [( )].</p><blockquote>Services and Dependency Injection</blockquote><blockquote><strong>Dependency Injection:<br></strong><em>In general, there are many ways an element can get a control of its dependencies.</em></blockquote><blockquote><em>In Angular project, a Component can ask Angular directly about dependencies with the other Modules. TypeScript compiler does all the work .<br>For more information about Typescript, Compiler and Transpiler please visit the web site of the software :</em><br><a href="https://www.typescriptlang.org/">TypeScript</a><br><a href="http://es6-features.org/#Constants">ECMASCRIPT 6</a></blockquote><p><strong>Service</strong></p><p><em>The key point to remember about a Services, those who having annotation </em><strong><em>@Injectable</em></strong><em> and they will be provided as a singleton when they are included in the array of </em><strong>providers</strong><em> in </em><strong><em>@NgModule </em></strong><em>class, we don’t care if it’s a root module or not.<br>Let’s look at an example:</em></p><pre>@Injectable()<br>export class BookService {<br>....</pre><pre>import { Component } from &#39;@angular/core&#39;;<br>import { BookService } from &#39;./book.service&#39;;<br>@Component({<br>    selector: &#39;book&#39;,<br>    templateUrl: &#39;book.component.html&#39;,<br>    styleUrls: [&#39;book.component.css&#39;]<br>})<br>export class BookComponent {<br>    constructor(private bookService: BookService) {<br>}</pre><pre>@NgModule({<br>  providers: []<br> // We must updating this array <br>})<br>export class AppModule { }</pre><p>The code won’t compile: We have references to BookService.<br>The compiler is happy to show us the problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*crikH8uc_7EvsmpMDCRsiw.png" /></figure><p>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.</p><pre>mport { NgModule } from &#39;@angular/core&#39;;<br> <br>@NgModule({<br>  providers: [],<br>  // NO Some Others providers<br>})<br>export class AppModule { }</pre><p>So, then, what is happening when this array seems to be changing?<br>Let’s find out.</p><pre>import { Injectable } from &#39;@angular/core&#39;;<br> <br>@Injectable( {<br>    providedIn: &#39;root&#39;,<br>} )<br>export class BookService {<br>....</pre><p>It’s not required that <strong>The providedIn: ‘root’</strong> solution be in all element <strong>@Injectable</strong>, we can simply inject needed services and we’re happy.</p><p>Directives</p><p>In this section, we’re going to do a really basic introduction to Directives.<br>We’re starting from the very beginning to <strong>MANIPULATE ALL THE DOM !!!<br></strong>As you’ve seen <strong>ROWSPAN</strong> &amp; <strong>COLSPAN</strong> are attributes of the two elements of table, <strong>th</strong> and <strong>td</strong> .<br>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.<br>For More about existing directive, please visit Angular web site.</p><p>Of course, the cool thing here is that any element in the DOM from any Component can also implement its own directive.<br>How do you know for sure whether a directive will be created? Because you didn’t write any class with annotation<strong> @Directive</strong> in your application.</p><p>The following is an example of <strong><em>read</em></strong> directive:</p><pre>import { Directive, ElementRef, Input} from &#39;@angular/core&#39;;<br>@Directive({<br>  selector: &#39;[read]&#39;<br>})<br>export class ReadDirective {<br>// set a constructor for class here. # line 6<br>// set a constructor for [read] here. # line 7<br>...</pre><p>* * * start to finish * * * *</p><p>In this pseudocode example, lines 6 means that you shoud know the source element.</p><pre>constructor( elementRef: ElementRef) {}</pre><p>And lines 7 means you should get a value from source element and do something.</p><pre>@Input() set read( value: any) { ... }</pre><p>Than In <strong><em>book.component.html</em></strong> we can inject this</p><pre>&lt;h2 [read]=&quot;{{book.author}}&quot;&gt;&lt;/h2&gt;</pre><p>Finish this point by updating the array of declarations in the root module.</p><p>…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a55963e8bb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-instance/articles-sur-angular-a55963e8bb">Introduction</a> was originally published in <a href="https://medium.com/angular-instance">Angular instance</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>