<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ angular8 - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ angular8 - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 19:50:34 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/angular8/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to build an Angular 8 app from scratch in 11 easy steps ]]>
                </title>
                <description>
                    <![CDATA[ By Ahmed Bouchefra Angular is one of the three most popular frameworks for front-end development, alongside React and Vue.js. The latest version is Angular 8 which was released on May 28, 2019.  There are many new features and enhancements to both th... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/angular-8-tutorial-in-easy-steps/</link>
                <guid isPermaLink="false">66d45d5cb3016bf139028d07</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular8 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Apps ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 11 Nov 2019 22:31:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/12/desktop-cropped-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ahmed Bouchefra</p>
<p>Angular is one of the three most popular frameworks for front-end development, alongside React and Vue.js. The latest version is Angular 8 which was released on May 28, 2019. </p>
<p>There are many new features and enhancements to both the command-line interface and the framework itself which result in a performance-boost and smaller production bundles. One interesting feature is the ng deploy command which allows developers to quickly build and deploy their Angular apps to popular hosting providers such as Firebase or GitHub.</p>
<p>In this tutorial, we’ll take you step by step on a journey to build an example Angular application from scratch that uses many Angular APIs such as HttpClient, and Material Design.</p>
<p>Here are a few things we'll learn:</p>
<ul>
<li>How to mock a REST API server that uses fake data from a JSON file </li>
<li>How to consume the REST API from our Angular 8 application using  <code>Httplient</code></li>
<li>How to handle HTTP errors using the RxJS  <code>throwError()</code>  and  <code>catchError()</code>  operators</li>
<li>How to retry failed HTTP requests in poor network conditions and cancel pending requests using the RxJS  <code>retry()</code>  and  <code>takeUntil()</code> operators</li>
<li>How to create and make use of Angular components and services </li>
<li>How to set up routing and Angular Material in our project and create a professional-looking UI with Material Design components</li>
<li>And finally, we’ll learn how to deploy the application to Firebase using the  <code>ng deploy</code>  command available in Angular 8.3+.</li>
</ul>
<p>You’ll also learn by example:</p>
<ul>
<li>How to quickly mock a REST API with real-world features, such as pagination, that you can consume from your app before you can switch to a real backend when it’s ready.</li>
<li>How to set up Angular CLI</li>
<li>How to initialize your Angular 8 project</li>
<li>How to set up Angular Material</li>
<li>How to add Angular components and routing</li>
<li>How to generate and use Angular services</li>
<li>How to consume REST APIs with Angular HttpClient</li>
<li>How to build and deploy your Angular application to Firebase.</li>
</ul>
<p>This tutorial is divided into the following steps:</p>
<ul>
<li>Step 1 — Installing Angular CLI 8</li>
<li>Step 2 — Creating your Angular 8 Project</li>
<li>Step 3 — Adding Angular HttpClient</li>
<li>Step 4 — Creating Components</li>
<li>Step 5 — Adding Routing</li>
<li>Step 6 — Building the UI with Angular Material Components</li>
<li>Step 7 — Mocking a REST API</li>
<li>Step 8 — Consuming the REST API with Angular HttpClient</li>
<li>Step 9 — Handling HTTP Errors</li>
<li>Step 10 — Adding Pagination</li>
<li>Step 11 — Building and Deploying your Angular Application to Firebase</li>
</ul>
<p>Now, let’s get started with the prerequisites!</p>
<blockquote>
<p><strong>Note</strong>: you can download our <strong><a target="_blank" href="https://www.techiediaries.com/angular-book-build-your-first-web-apps/">Angular 8 Book: Build your first web apps with Angular 8</a> </strong> for free.</p>
</blockquote>
<h1 id="heading-prerequisites">Prerequisites</h1>
<p>If you want to follow this tutorial, you’ll need to have:</p>
<ul>
<li>Prior knowledge of TypeScript.</li>
<li>A development machine with  <strong>Node 8.9+</strong> and  <strong>NPM 5.5.1+</strong>  installed. Node is required by the Angular CLI. You can head to <a target="_blank" href="https://nodejs.org/downloads">the official website</a> and grab the binaries for your system. You can also use  <a target="_blank" href="https://github.com/nvm-sh/nvm">NVM</a>  — Node Version Manager — a POSIX-compliant bash script to install and manage multiple Node.js versions in your machine.</li>
</ul>
<p>If you are ready, let’s learn by example how to build an Angular 8 application that consumes a REST API using HttpClient. We'll implement real-world features like error handling and retrying failed HTTP requests.</p>
<h1 id="heading-step-1-installing-angular-cli-8">Step 1 — Installing Angular CLI 8</h1>
<p>Let’s start with the first step, where we’ll install the latest version of Angular CLI.</p>
<p><img src="https://www.techiediaries.com/ezoimgfmt/www.diigo.com/file/image/rscqpoqzoceeaeedqzdspasasb/Angular+CLI+8.jpg?ezimgfmt=rs:461x281/rscb1/ng:webp/ngcb1" alt="Image" width="600" height="400" loading="lazy"></p>
<p><a target="_blank" href="https://cli.angular.io/">Angular CLI</a>  is the official tool for initializing and working with Angular projects. Head to a new terminal and execute the following command:</p>
<pre><code class="lang-bash">$ npm install -g @angular/cli
</code></pre>
<p>When writing this tutorial,  <strong>angular/cli v8.3.2</strong>  is installed on our system.</p>
<p>That’s it, you are ready for the second step!</p>
<h1 id="heading-step-2-creating-your-angular-8-project">Step 2 — Creating your Angular 8 Project</h1>
<p>In this step, we’ll use Angular CLI to initialize our Angular project.</p>
<p>Go to your terminal and execute these commands:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">cd</span> ~  
$ ng new angular-example
</code></pre>
<p>The CLI will prompt you and ask <strong>whether you would like to add Angular routing.</strong> Say Yes. It'll then ask <strong>which stylesheet format you would like to use.</strong> Choose <strong>CSS</strong>.</p>
<p>Angular CLI will generate the required files and folders, install the packages from npm, and even automatically set up routing in our project.</p>
<p>Now, go to your project’s root folder and run the local development server using these commands:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">cd</span> angular-example  
$ ng serve
</code></pre>
<p>Your Angular web application will be available from the  <code>[http://localhost:4200/](http://localhost:4200/)</code>  address.</p>
<p><img src="https://www.techiediaries.com/ezoimgfmt/www.diigo.com/file/image/rscqpoqzoceeaposbzdspascea/Angular+CLI+Ng+Serve.jpg?ezimgfmt=rs:710x191/rscb1/ng:webp/ngcb1" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Open a web browser and go to the  <code>http://localhost:4200/</code> address. You should see this beautiful page (Starting with Angular 8.3+):</p>
<p><img src="https://www.techiediaries.com/ezoimgfmt/paper-attachments.dropbox.com/s_F52E295BB9C92BEFE7506DFCE2086C2583C762072AFE2CA1A9CE9AD4DA9FF751_1567465432228_Angulardemo.png?ezimgfmt=rs:710x341/rscb1/ng:webp/ngcb1" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You need to leave the development server running and open a new terminal for the next steps.</p>
<p>You are now ready for the third step!</p>
<h1 id="heading-step-3-adding-angular-httpclient">Step 3 — Adding Angular HttpClient</h1>
<p>In this step, we’ll add  <code>HttpClient</code>  to our example project.</p>
<p>Open the  <code>src/app/app.module.ts</code>  file and make the following changes:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { BrowserModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/platform-browser'</span>;
<span class="hljs-keyword">import</span> { NgModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-keyword">import</span> { AppRoutingModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'./app-routing.module'</span>;
<span class="hljs-keyword">import</span> { AppComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./app.component'</span>;
<span class="hljs-keyword">import</span> { HttpClientModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common/http'</span>;

<span class="hljs-meta">@NgModule</span>({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppModule { }
</code></pre>
<p>We simply imported <a target="_blank" href="https://angular.io/api/common/http/HttpClientModule#description">HttpClientModule</a> and included it in the <code>imports</code> array.</p>
<p>That’s all - now we can use the  <code>HttpClient</code>  service in our Angular project to consume our REST API.</p>
<p>You are ready for the fifth step!</p>
<h1 id="heading-step-4-creating-ui-components">Step 4 — Creating UI Components</h1>
<p>Angular apps are made up of components. In this step, we’ll learn how to create a couple of Angular components that compose our UI.</p>
<p>Open a new terminal and run the following command:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">cd</span> ~/angular-example  
$ ng g component home
</code></pre>
<p>You’ll get the following output in your terminal:</p>
<pre><code>CREATE src/app/home/home.component.html (<span class="hljs-number">19</span> bytes)  
CREATE src/app/home/home.component.spec.ts (<span class="hljs-number">614</span> bytes)  
CREATE src/app/home/home.component.ts (<span class="hljs-number">261</span> bytes)  
CREATE src/app/home/home.component.css (<span class="hljs-number">0</span> bytes)  
UPDATE src/app/app.module.ts (<span class="hljs-number">467</span> bytes)
</code></pre><p>We have four files, all required by our component.</p>
<p>Next, generate the about component:</p>
<pre><code class="lang-bash">$ ng g component about
</code></pre>
<p>Next, open the  <code>src/app/about/about.component.html</code>  file and add the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"padding: 15px;"</span>&gt;</span> This is the about page that describes your app<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>You are ready for the sixth step!</p>
<h1 id="heading-step-5-adding-routing">Step 5 — Adding Routing</h1>
<p>In this step, we’ll learn how to add routing to our example.</p>
<p>Go to the  <code>src/app/app-routing.module.ts</code>  file and add the following routes:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { NgModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { Routes, RouterModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/router'</span>;
<span class="hljs-keyword">import</span> { HomeComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./home/home.component'</span>;
<span class="hljs-keyword">import</span> { AboutComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./about/about.component'</span>;


<span class="hljs-keyword">const</span> routes: Routes = [
  { path: <span class="hljs-string">''</span>, redirectTo: <span class="hljs-string">'home'</span>, pathMatch: <span class="hljs-string">'full'</span>},
  { path: <span class="hljs-string">'home'</span>, component: HomeComponent },
  { path: <span class="hljs-string">'about'</span>, component: AboutComponent },
];

<span class="hljs-meta">@NgModule</span>({
  imports: [RouterModule.forRoot(routes)],
  <span class="hljs-built_in">exports</span>: [RouterModule]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppRoutingModule { }
</code></pre>
<p>We imported the Angular components and we declared three routes.</p>
<p>The first route is for redirecting the empty path to the home component, so we’ll be automatically redirected to the home page when we first visit the app.</p>
<p>That’s it. Now that you have added routing, you are ready for the next step!</p>
<h1 id="heading-step-6-adding-angular-material">Step 6 — Adding Angular Material</h1>
<p>In this tutorial step, we’ll learn to set up <a target="_blank" href="https://material.angular.io/">Angular Material</a> in our project and build our application UI using Material components.</p>
<p>Go to your terminal and run this command from the root of your project:</p>
<pre><code class="lang-bash">$ ng add @angular/material
</code></pre>
<p>You’ll be prompted to choose the theme, so let’s pick <strong>Indigo/Pink</strong>.</p>
<p>For the other questions - whether you want to <strong>set up HammerJS for gesture recognition</strong> and if you want to <strong>set up browser animations for Angular Material</strong> - press <strong>Enter</strong> to use the default answers.</p>
<p>Open the  <code>src/app/app.module.ts</code>  file and add the following imports:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { MatToolbarModule,
  MatIconModule,
  MatCardModule,
  MatButtonModule,
  MatProgressSpinnerModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/material'</span>;
</code></pre>
<p>We imported the modules for these Material Design components:</p>
<ul>
<li><a target="_blank" href="https://material.angular.io/components/toolbar/overview">MatToolbar</a> that provides a container for headers, titles, or actions.</li>
<li><a target="_blank" href="https://material.angular.io/components/card/overview">MatCard</a> that provides a content container for text, photos, and actions in the context of a single subject.</li>
<li><a target="_blank" href="https://material.angular.io/components/button/overview">MatButton</a> that provides a native  <code>&lt;button&gt;</code>  or  <code>&lt;a&gt;</code>  element enhanced with Material Design styling and ink ripples.</li>
<li><a target="_blank" href="https://material.angular.io/components/progress-spinner/overview">MatProgressSpinner</a> that provides a circular indicator of progress and activity.</li>
</ul>
<p>Next, add these modules to the  <code>imports</code>  array:</p>
<pre><code class="lang-ts"><span class="hljs-meta">@NgModule</span>({
  declarations: [
    AppComponent,
    HomeComponent,
    AboutComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    BrowserAnimationsModule,
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatCardModule,
    MatProgressSpinnerModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppModule { }
</code></pre>
<p>Next, open the  <code>src/app/app.component.html</code>  file and update it as follows:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">mat-toolbar</span> <span class="hljs-attr">color</span>=<span class="hljs-string">"primary"</span>&gt;</span>  
<span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>  
My Angular Store  
<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>  
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">mat-button</span> <span class="hljs-attr">routerLink</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>  
<span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">mat-button</span> <span class="hljs-attr">routerLink</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mat-toolbar</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">router-outlet</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-outlet</span>&gt;</span>
</code></pre>
<p>We added a top navigation bar with two buttons that take us to the home and about pages, respectively.</p>
<h1 id="heading-step-7-mocking-a-rest-api">Step 7 — Mocking a REST API</h1>
<p>Go to a new command-line interface and start by installing  <code>json-server</code>  from npm in your project:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">cd</span> ~/angular-example
$ npm install --save json-server
</code></pre>
<p>Next, create a  <code>server</code>  folder in the root folder of your Angular project:</p>
<pre><code class="lang-bash">$ mkdir server
$ <span class="hljs-built_in">cd</span> server
</code></pre>
<p>In the  <code>server</code>  folder, create a  <code>database.json</code>  file and add the following JSON object:</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"products"</span>: []
}
</code></pre>
<p>This JSON file will act as a database for your REST API server. You can simply add some data to be served by your REST API or use <a target="_blank" href="https://github.com/marak/Faker.js/">Faker.js</a> for automatically generating massive amounts of realistic fake data.</p>
<p>Go back to your command-line, navigate back from the  <code>server</code>  folder, and install <code>Faker.js</code>  from npm using the following command:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">cd</span> ..
$ npm install faker --save
</code></pre>
<p>At the time of creating this example, <strong>faker v4.1.0</strong> will be installed.</p>
<p>Now, create a  <code>generate.js</code>  file and add the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> faker = <span class="hljs-built_in">require</span>(<span class="hljs-string">'faker'</span>);

<span class="hljs-keyword">var</span> database = { <span class="hljs-attr">products</span>: []};

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">1</span>; i&lt;= <span class="hljs-number">300</span>; i++) {
  database.products.push({
    <span class="hljs-attr">id</span>: i,
    <span class="hljs-attr">name</span>: faker.commerce.productName(),
    <span class="hljs-attr">description</span>: faker.lorem.sentences(),
    <span class="hljs-attr">price</span>: faker.commerce.price(),
    <span class="hljs-attr">imageUrl</span>: <span class="hljs-string">"https://source.unsplash.com/1600x900/?product"</span>,
    <span class="hljs-attr">quantity</span>: faker.random.number()
  });
}

<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(database));
</code></pre>
<p>We first imported faker, and next we defined an object with one empty array for products. Next, we entered a <em>for</em> loop to create <em>300</em> fake entries using faker methods like  <code>faker.commerce.productName()</code>  for generating product names. <a target="_blank" href="https://github.com/marak/Faker.js/#api-methods">Check all the available methods</a>. Finally we converted the database object to a string and logged it to standard output.</p>
<p>Next, add the  <code>generate</code>  and  <code>server</code>  scripts to the  <code>package.json</code>  file:</p>
<pre><code class="lang-json">  <span class="hljs-string">"scripts"</span>: {
    <span class="hljs-attr">"ng"</span>: <span class="hljs-string">"ng"</span>,
    <span class="hljs-attr">"start"</span>: <span class="hljs-string">"ng serve"</span>,
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"ng build"</span>,
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"ng test"</span>,
    <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"ng lint"</span>,
    <span class="hljs-attr">"e2e"</span>: <span class="hljs-string">"ng e2e"</span>,
    <span class="hljs-attr">"generate"</span>: <span class="hljs-string">"node ./server/generate.js &gt; ./server/database.json"</span>,
    <span class="hljs-attr">"server"</span>: <span class="hljs-string">"json-server --watch ./server/database.json"</span>
  },
</code></pre>
<p>Next, head back to your command-line interface and run the generate script using the following command:</p>
<pre><code class="lang-bash">$ npm run generate
</code></pre>
<p>Finally, run the REST API server by executing the following command:</p>
<pre><code class="lang-bash">$ npm run server
</code></pre>
<p>You can now send HTTP requests to the server just like any typical REST API server. Your server will be available from the  <code>http://localhost:3000/</code>  address.</p>
<p>These are the API endpoints we'll be able to use via our JSON REST API server:</p>
<ul>
<li><code>GET /products</code>  for getting the products</li>
<li><code>GET /products/&lt;id&gt;</code>  for getting a single product by id</li>
<li><code>POST /products</code>  for creating a new product</li>
<li><code>PUT /products/&lt;id&gt;</code>  for updating a product by id</li>
<li><code>PATCH /products/&lt;id&gt;</code>  for partially updating a product by id</li>
<li><code>DELETE /products/&lt;id&gt;</code>  for deleting a product by id</li>
</ul>
<p>You can use  <code>_page</code>  and  <code>_limit</code>  parameters to get paginated data. In the  <code>Link</code> header you'll get  <code>first</code>,  <code>prev</code>,  <code>next</code>  and  <code>last</code>  links.</p>
<p>Leave the JSON REST API server running and open a new command-line interface for typing the commands of the next steps.</p>
<h1 id="heading-step-8-creating-a-service-for-consuming-the-rest-api-with-angular-httpclient">Step 8 — Creating a Service for Consuming the REST API with Angular HttpClient</h1>
<p>In this step, we’ll learn how to consume our REST API from Angular using HttpClient.</p>
<p>We’ll need to create an Angular service for encapsulating the code that allows us to consume data from our REST API server.</p>
<p>Go to your terminal and run the following command:</p>
<pre><code class="lang-bash">$ ng g service api
</code></pre>
<p>Next, go to the  <code>src/app/api.service.ts</code>  file, import and inject  <code>HttpClient</code>:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Injectable } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { HttpClient } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common/http'</span>;

<span class="hljs-meta">@Injectable</span>({
  providedIn: <span class="hljs-string">'root'</span>
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> ApiService {

  <span class="hljs-keyword">private</span> SERVER_URL = <span class="hljs-string">"http://localhost:3000"</span>;

  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> httpClient: HttpClient</span>) { }
}
</code></pre>
<p>We imported and injected the  <code>HttpClient</code>  service and defined the  <code>SERVER_URL</code> variable that contains the address of our REST API server.</p>
<p>Next, define a  <code>get()</code>  method that sends a GET request to the REST API endpoint:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Injectable } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  
<span class="hljs-keyword">import</span> { HttpClient } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common/http'</span>;

<span class="hljs-meta">@Injectable</span>({  
    providedIn: <span class="hljs-string">'root'</span>  
})  
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> ApiService {

    <span class="hljs-keyword">private</span> SERVER_URL = <span class="hljs-string">"http://localhost:3000"</span>;
    <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> httpClient: HttpClient</span>) { }

    <span class="hljs-keyword">public</span> get(){  
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.httpClient.get(<span class="hljs-built_in">this</span>.SERVER_URL);  
    }  
}
</code></pre>
<p>The method simply invokes the  <code>get()</code>  method of  <code>HttpClient</code>  to send GET requests to the REST API server.</p>
<p>Next, we now need to use this service in our home component. Open the <code>src/app/home/home.component.ts</code>  file, and import and inject the data service as follows:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, OnInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  
<span class="hljs-keyword">import</span> { ApiService } <span class="hljs-keyword">from</span> <span class="hljs-string">'../api.service'</span>;

<span class="hljs-meta">@Component</span>({  
    selector: <span class="hljs-string">'app-home'</span>,  
    templateUrl: <span class="hljs-string">'./home.component.html'</span>,  
    styleUrls: [<span class="hljs-string">'./home.component.css'</span>]  
})  
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HomeComponent <span class="hljs-keyword">implements</span> OnInit {
    products = [];
    <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> apiService: ApiService</span>) { }
    ngOnInit() {
        <span class="hljs-built_in">this</span>.apiService.get().subscribe(<span class="hljs-function">(<span class="hljs-params">data: <span class="hljs-built_in">any</span>[]</span>)=&gt;</span>{  
            <span class="hljs-built_in">console</span>.log(data);  
            <span class="hljs-built_in">this</span>.products = data;  
        })  
    }
}
</code></pre>
<p>We imported and injected  <code>ApiService.</code>  Next, we defined a  <code>products</code>  variable and called the  <code>get()</code>  method of the service for fetching data from the JSON REST API server.</p>
<p>Next, open the  <code>src/app/home/home.component.html</code>  file and update it as follows:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"padding: 13px;"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">mat-spinner</span> *<span class="hljs-attr">ngIf</span>=<span class="hljs-string">"products.length === 0"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mat-spinner</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">mat-card</span> *<span class="hljs-attr">ngFor</span>=<span class="hljs-string">"let product of products"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin-top:10px;"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-header</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-title</span>&gt;</span>{{product.name}}<span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-title</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-subtitle</span>&gt;</span>{{product.price}} $/ {{product.quantity}}
            <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-subtitle</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-header</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-content</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                {{product.description}}
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"height:100%; width: 100%;"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"{{ product.imageUrl }}"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-content</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-actions</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">mat-button</span>&gt;</span> Buy product<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-actions</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We used the  <code>&lt;mat-spinner&gt;</code>  component for showing a loading spinner when the length of the  <code>products</code>  array equals zero, that is before any data is received from the REST API server.</p>
<p>Next, we iterated over the  <code>products</code>  array and used a Material card to display the <code>name</code>,  <code>price</code>,  <code>quantity</code>,  <code>description</code>  and  <code>image</code>  of each product.</p>
<p>This is a screenshot of the home page after JSON data is fetched:</p>
<p><img src="https://miro.medium.com/max/301/0*R7qs5jGg_IlOtTWF" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we’ll see how to add error handling to our service.</p>
<h1 id="heading-step-9-adding-error-handling">Step 9 — Adding Error Handling</h1>
<p>In this step, we’ll learn to add error handling in our example.</p>
<p>Go to the  <code>src/app/api.service.ts</code>  file and update it as follows:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Injectable } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { HttpClient, HttpErrorResponse } <span class="hljs-keyword">from</span> <span class="hljs-string">"@angular/common/http"</span>;

<span class="hljs-keyword">import</span> {  throwError } <span class="hljs-keyword">from</span> <span class="hljs-string">'rxjs'</span>;
<span class="hljs-keyword">import</span> { retry, catchError } <span class="hljs-keyword">from</span> <span class="hljs-string">'rxjs/operators'</span>;


<span class="hljs-meta">@Injectable</span>({
  providedIn: <span class="hljs-string">'root'</span>
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> ApiService {

  <span class="hljs-keyword">private</span> SERVER_URL = <span class="hljs-string">"http://localhost:3000/products"</span>;

  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> httpClient: HttpClient</span>) { }

  handleError(error: HttpErrorResponse) {
    <span class="hljs-keyword">let</span> errorMessage = <span class="hljs-string">'Unknown error!'</span>;
    <span class="hljs-keyword">if</span> (error.error <span class="hljs-keyword">instanceof</span> ErrorEvent) {
      <span class="hljs-comment">// Client-side errors</span>
      errorMessage = <span class="hljs-string">`Error: <span class="hljs-subst">${error.error.message}</span>`</span>;
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// Server-side errors</span>
      errorMessage = <span class="hljs-string">`Error Code: <span class="hljs-subst">${error.status}</span>\nMessage: <span class="hljs-subst">${error.message}</span>`</span>;
    }
    <span class="hljs-built_in">window</span>.alert(errorMessage);
    <span class="hljs-keyword">return</span> throwError(errorMessage);
  }

  <span class="hljs-keyword">public</span> sendGetRequest(){
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.httpClient.get(<span class="hljs-built_in">this</span>.SERVER_URL).pipe(catchError(<span class="hljs-built_in">this</span>.handleError));
  }
}
</code></pre>
<p>This is a screenshot of an example error on the browser console:</p>
<p><img src="https://miro.medium.com/max/442/0*gZUHzXPjrRSSK4ZF" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the next step, we’ll see how to add pagination to our application</p>
<h1 id="heading-step-10-adding-pagination">Step 10 — Adding Pagination</h1>
<p>In this step, we’ll learn to add support for data pagination using the Link header of the HTTP response received from the REST API server.</p>
<p>By default, HttpClient provides only the response body. But in our app we need to parse the Link header for extracting pagination links. So we need to instruct<code>HttpClient</code>  to give us the full <a target="_blank" href="https://angular.io/api/common/http/HttpResponse">HttpResponse</a> using the  <code>observe</code>  option.</p>
<p>The Link header in HTTP allows the server to point an interested client to another resource containing metadata about the requested resource. <a target="_blank" href="https://www.w3.org/wiki/LinkHeader">Wikipedia</a></p>
<p>Open the <code>src/app/data.service.ts</code> file and import the RxJS  <code>tap()</code>  operator:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { retry, catchError, tap } <span class="hljs-keyword">from</span> <span class="hljs-string">'rxjs/operators'</span>;
</code></pre>
<p>Next, add these variables:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">public</span> first: <span class="hljs-built_in">string</span> = <span class="hljs-string">""</span>;  
<span class="hljs-keyword">public</span> prev: <span class="hljs-built_in">string</span> = <span class="hljs-string">""</span>;  
<span class="hljs-keyword">public</span> next: <span class="hljs-built_in">string</span> = <span class="hljs-string">""</span>;  
<span class="hljs-keyword">public</span> last: <span class="hljs-built_in">string</span> = <span class="hljs-string">""</span>;
</code></pre>
<p>Next, add the  <code>parseLinkHeader()</code>  method that will be used to parse the Link header and populates the previous variables:</p>
<pre><code class="lang-ts">  parseLinkHeader(header) {
    <span class="hljs-keyword">if</span> (header.length == <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">return</span> ;
    }

    <span class="hljs-keyword">let</span> parts = header.split(<span class="hljs-string">','</span>);
    <span class="hljs-keyword">var</span> links = {};
    parts.forEach( <span class="hljs-function"><span class="hljs-params">p</span> =&gt;</span> {
      <span class="hljs-keyword">let</span> section = p.split(<span class="hljs-string">';'</span>);
      <span class="hljs-keyword">var</span> url = section[<span class="hljs-number">0</span>].replace(<span class="hljs-regexp">/&lt;(.*)&gt;/</span>, <span class="hljs-string">'$1'</span>).trim();
      <span class="hljs-keyword">var</span> name = section[<span class="hljs-number">1</span>].replace(<span class="hljs-regexp">/rel="(.*)"/</span>, <span class="hljs-string">'$1'</span>).trim();
      links[name] = url;

    });

    <span class="hljs-built_in">this</span>.first  = links[<span class="hljs-string">"first"</span>];
    <span class="hljs-built_in">this</span>.last   = links[<span class="hljs-string">"last"</span>];
    <span class="hljs-built_in">this</span>.prev   = links[<span class="hljs-string">"prev"</span>];
    <span class="hljs-built_in">this</span>.next   = links[<span class="hljs-string">"next"</span>]; 
  }
</code></pre>
<p>Next, update the  <code>sendGetRequest()</code>  as follows:</p>
<pre><code class="lang-ts">  <span class="hljs-keyword">public</span> sendGetRequest(){
    <span class="hljs-comment">// Add safe, URL encoded _page and _limit parameters </span>

    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.httpClient.get(<span class="hljs-built_in">this</span>.SERVER_URL, {  params: <span class="hljs-keyword">new</span> HttpParams({fromString: <span class="hljs-string">"_page=1&amp;_limit=20"</span>}), observe: <span class="hljs-string">"response"</span>}).pipe(retry(<span class="hljs-number">3</span>), catchError(<span class="hljs-built_in">this</span>.handleError), tap(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> {
      <span class="hljs-built_in">console</span>.log(res.headers.get(<span class="hljs-string">'Link'</span>));
      <span class="hljs-built_in">this</span>.parseLinkHeader(res.headers.get(<span class="hljs-string">'Link'</span>));
    }));
  }
</code></pre>
<p>We added the  <code>observe</code>  option with the  <code>response</code>  value in the options parameter of the  <code>get()</code>  method so we can have the full HTTP response with headers. Next, we use the RxJS  <code>tap()</code>  operator for parsing the Link header before returning the final Observable.</p>
<p>Since the  <code>sendGetRequest()</code>  is now returning an Observable with a full HTTP response, we need to update the home component so open the  <code>src/app/home/home.component.ts</code>  file and import  <code>HttpResponse</code>  as follows:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { HttpResponse } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common/http'</span>;
</code></pre>
<p>Next, update the  <code>subscribe()</code>  method as follows:</p>
<pre><code class="lang-ts">ngOnInit(){

<span class="hljs-built_in">this</span>.apiService.sendGetRequest().pipe(takeUntil(<span class="hljs-built_in">this</span>.destroy$)).subscribe(<span class="hljs-function">(<span class="hljs-params">res: HttpResponse&lt;<span class="hljs-built_in">any</span>&gt;</span>)=&gt;</span>{  
    <span class="hljs-built_in">console</span>.log(res);  
    <span class="hljs-built_in">this</span>.products = res.body;  
})  
}
</code></pre>
<p>We can now access the data from the  <code>body</code>  object of the received HTTP response.</p>
<p>Next, go back to the src/app/data.service.ts file and add the following method:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">public</span> sendGetRequestToUrl(url: <span class="hljs-built_in">string</span>){  
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.httpClient.get(url, { observe: <span class="hljs-string">"response"</span>}).pipe(retry(<span class="hljs-number">3</span>),             
    catchError(<span class="hljs-built_in">this</span>.handleError), tap(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> {  
        <span class="hljs-built_in">console</span>.log(res.headers.get(<span class="hljs-string">'Link'</span>));  
        <span class="hljs-built_in">this</span>.parseLinkHeader(res.headers.get(<span class="hljs-string">'Link'</span>));
    }));  
}
</code></pre>
<p>This method is similar to  <code>sendGetRequest()</code>  except that it takes the URL to which we need to send an HTTP GET request.</p>
<p>Go back to the  <code>src/app/home/home.component.ts</code>  file and add define the following methods:</p>
<pre><code class="lang-ts"> <span class="hljs-keyword">public</span> firstPage() {
    <span class="hljs-built_in">this</span>.products = [];
    <span class="hljs-built_in">this</span>.apiService.sendGetRequestToUrl(<span class="hljs-built_in">this</span>.apiService.first).pipe(takeUntil(<span class="hljs-built_in">this</span>.destroy$)).subscribe(<span class="hljs-function">(<span class="hljs-params">res: HttpResponse&lt;<span class="hljs-built_in">any</span>&gt;</span>) =&gt;</span> {
      <span class="hljs-built_in">console</span>.log(res);
      <span class="hljs-built_in">this</span>.products = res.body;
    })
  }
  <span class="hljs-keyword">public</span> previousPage() {

    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.apiService.prev !== <span class="hljs-literal">undefined</span> &amp;&amp; <span class="hljs-built_in">this</span>.apiService.prev !== <span class="hljs-string">''</span>) {
      <span class="hljs-built_in">this</span>.products = [];
      <span class="hljs-built_in">this</span>.apiService.sendGetRequestToUrl(<span class="hljs-built_in">this</span>.apiService.prev).pipe(takeUntil(<span class="hljs-built_in">this</span>.destroy$)).subscribe(<span class="hljs-function">(<span class="hljs-params">res: HttpResponse&lt;<span class="hljs-built_in">any</span>&gt;</span>) =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(res);
        <span class="hljs-built_in">this</span>.products = res.body;
      })
    }

  }
  <span class="hljs-keyword">public</span> nextPage() {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.apiService.next !== <span class="hljs-literal">undefined</span> &amp;&amp; <span class="hljs-built_in">this</span>.apiService.next !== <span class="hljs-string">''</span>) {
      <span class="hljs-built_in">this</span>.products = [];
      <span class="hljs-built_in">this</span>.apiService.sendGetRequestToUrl(<span class="hljs-built_in">this</span>.apiService.next).pipe(takeUntil(<span class="hljs-built_in">this</span>.destroy$)).subscribe(<span class="hljs-function">(<span class="hljs-params">res: HttpResponse&lt;<span class="hljs-built_in">any</span>&gt;</span>) =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(res);
        <span class="hljs-built_in">this</span>.products = res.body;
      })
    }
  }
  <span class="hljs-keyword">public</span> lastPage() {
    <span class="hljs-built_in">this</span>.products = [];
    <span class="hljs-built_in">this</span>.apiService.sendGetRequestToUrl(<span class="hljs-built_in">this</span>.apiService.last).pipe(takeUntil(<span class="hljs-built_in">this</span>.destroy$)).subscribe(<span class="hljs-function">(<span class="hljs-params">res: HttpResponse&lt;<span class="hljs-built_in">any</span>&gt;</span>) =&gt;</span> {
      <span class="hljs-built_in">console</span>.log(res);
      <span class="hljs-built_in">this</span>.products = res.body;
    })
  }
</code></pre>
<p>Finally, open the  <code>src/app/home/home.component.html</code>  file and update the template as follows:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"padding: 13px;"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">mat-spinner</span> *<span class="hljs-attr">ngIf</span>=<span class="hljs-string">"products.length === 0"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">mat-spinner</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">mat-card</span> *<span class="hljs-attr">ngFor</span>=<span class="hljs-string">"let product of products"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"margin-top:10px;"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-header</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-title</span>&gt;</span>#{{product.id}} {{product.name}}<span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-title</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-subtitle</span>&gt;</span>{{product.price}} $/ {{product.quantity}}
            <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-subtitle</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-header</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-content</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
                {{product.description}}
            <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"height:100%; width: 100%;"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"{{ product.imageUrl }}"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-content</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">mat-card-actions</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">mat-button</span>&gt;</span> Buy product<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card-actions</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">mat-card</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>) =<span class="hljs-string">"firstPage()"</span> <span class="hljs-attr">mat-button</span>&gt;</span> First<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>) =<span class="hljs-string">"previousPage()"</span> <span class="hljs-attr">mat-button</span>&gt;</span> Previous<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>) =<span class="hljs-string">"nextPage()"</span> <span class="hljs-attr">mat-button</span>&gt;</span> Next<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>) =<span class="hljs-string">"lastPage()"</span> <span class="hljs-attr">mat-button</span>&gt;</span> Last<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>This is a screenshot of our application:</p>
<p><img src="https://miro.medium.com/max/17/0*c_21mFswM-ZiReUi?q=20" alt="Image" width="600" height="400" loading="lazy"></p>
<p><img src="https://miro.medium.com/max/297/0*c_21mFswM-ZiReUi" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-step-11-building-and-deploying-your-angular-application-to-firebase">Step 11 — Building and Deploying your Angular Application to Firebase</h1>
<p>Head back to your command-line interface. Make sure you are inside the root folder of your Angular project and run the following command:</p>
<pre><code class="lang-bash">$ ng add @angular/fire
</code></pre>
<p>This will add the Firebase deployment capability to your project.</p>
<p>As the time of writing this tutorial, <strong>@angular/fire v5.2.1</strong> will be installed.</p>
<p>The command will also update the  <code>package.json</code>  of our project by adding this section:</p>
<pre><code class="lang-json">        <span class="hljs-string">"deploy"</span>: {
          <span class="hljs-attr">"builder"</span>: <span class="hljs-string">"@angular/fire:deploy"</span>,
          <span class="hljs-attr">"options"</span>: {}
        }
</code></pre>
<p>The CLI will prompt you to <strong>Paste authorization code here:</strong> and will open your default web browser and ask you to give Firebase CLI permissions to administer your Firebase account.</p>
<p>After you sign in with the Google account associated with your Firebase account, you'll be given the authorization code.</p>
<p>Next, you'll be prompted to <strong>Please select a project: (Use arrow keys or type to search)</strong>. You should have created a Firebase project before.</p>
<p>The CLI will create the  <code>firebase.json</code>  and  <code>.firebaserc</code>  files and update the <code>angular.json</code>  file accordingly.</p>
<p>Next, deploy your application to Firebase using the following command:</p>
<pre><code class="lang-bash">$ ng deploy
</code></pre>
<p>The command will produce an optimized build of your application (equivalent to the <code>ng deploy --prod</code>  command). It will upload the production assets to Firebase hosting.</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Throughout this step by step tutorial, you learned to build an Angular application from scratch using the latest Angular 8.3+ version.</p>
<p>You learned to mock a REST API backend for your Angular application with nearly zero lines of code.</p>
<p>You learned how to create a project using Angular CLI, add  <code>HttpClient</code>  and Angular Material for sending HTTP requests to your mocked REST API backend, and style the UI with Material Design components.</p>
<p>Finally, you learned to deploy your Angular application to Firebase using the  <code>ng deploy</code>  command available starting from Angular 8.3+.</p>
<p>Check out our other <a target="_blank" href="https://www.techiediaries.com/angular">Angular tutorials</a>.</p>
<p>You can reach out to or follow the author via his:</p>
<ul>
<li><a target="_blank" href="https://www.ahmedbouchefra.com/">Personal website</a></li>
<li><a target="_blank" href="https://twitter.com/ahmedbouchefra">Twitter</a></li>
<li><a target="_blank" href="https://www.linkedin.com/in/mr-ahmed/">LinkedIn</a></li>
<li><a target="_blank" href="https://github.com/techiediaries">Github</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Angular vs React: Which One to Choose for Your App ]]>
                </title>
                <description>
                    <![CDATA[ A precise side-by-side comparison of general and technical aspects of Angular and React There are so many articles titled “Angular vs React”, “React vs Angular”, “Angular or React” – it is a miracle you opened this one! What these articles are missin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/angular-vs-react-what-to-choose-for-your-app-2/</link>
                <guid isPermaLink="false">66be14af9f6cdc38e7dec50b</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular 5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular2 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular6 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular8 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Oleh Romanyuk ]]>
                </dc:creator>
                <pubDate>Tue, 08 Oct 2019 13:49:33 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/10/react-vs-angular.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <h3 id="heading-a-precise-side-by-side-comparison-of-general-and-technical-aspects-of-angular-and-react">A precise side-by-side comparison of general and technical aspects of Angular and React</h3>
<p>There are so many articles titled “Angular vs React”, “React vs Angular”, “Angular or React” – it is a miracle you opened this one! What these articles are missing, however, is a precise <strong>side-by-side comparison</strong> of Angular vs React. </p>
<blockquote>
<p>So this is what I am going to do in this blog post: to place <a target="_blank" href="https://keenethics.com/tech-front-end-react">React</a> and <a target="_blank" href="https://keenethics.com/tech-front-end-angular">Angular</a> in direct juxtaposition. We're going to review and contrast the two JavaScript frameworks and look at each possible characteristic to make sure we don't miss even a single piece of data. </p>
</blockquote>
<p>In the end, I am not going to tell you which technology to choose. But I will give you enough food for thought for you to choose the technology that suits you and your project best.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/08/Angular_React_comparison-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/2PpsrJKGRiYFYhCXh9NcfM/c43b9b24509d459ec94d7f7bb6ce207a/search__1_.png?h=300" alt="search (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-1-brief-overview">1. Brief Overview</h2>
<h3 id="heading-react">React</h3>
<p>React is a JavaScript library for UI development. It is managed by Facebook and an open-source community of developers.</p>
<p>The framework was introduced in May 2013.</p>
<p>The latest updates were released on August 8th, 2019 – just over a month ago.</p>
<h3 id="heading-angular">Angular</h3>
<p>Angular is an open-sourced JavaScript framework for web and mobile development. It is TypeScript-based and managed by Google’s Angular Team and the Angular developer community. </p>
<p>Launched in September 2016, Angular (also known as Angular 2.0) is a complete rewrite of AngularJS (Angular 1.0), which was introduced in 2010. </p>
<p>There have been six versions of Angular already, and the latest release took place on August 28th, 2019 – almost three weeks ago.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/26X0F8SysU9aRODLpiTS8T/6d17ff52d723a3d8b34b54a4f53f6e34/internet__1_.png?h=300" alt="internet (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-2-universality">2. Universality</h2>
<h3 id="heading-react-1">React</h3>
<p>React is a framework used in both web and mobile development. However, for mobile development, it needs to be incorporated with Cordova. Moreover, for mobile development, there is an additional framework – React Native.</p>
<p>React can be used to build both single-page and multiple-page web applications.</p>
<h3 id="heading-angular-1">Angular</h3>
<p>Angular is suitable for both web and mobile development. In mobile development, however, a great share of work is done by Ionic. Furthermore, similarly to React, Angular has an additional mobile development framework. The counterpart of React Native is NativeScript. </p>
<p>Angular can also be used for both single- and multiple-page web apps.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/7EP4F57YmLWZvF7OO0iDTX/b7ba3aacd67d576f1fcc49ba78478916/success__1_.png?h=300" alt="success (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-3-self-sufficiency">3. Self-Sufficiency</h2>
<h3 id="heading-react-2">React</h3>
<p>React is a framework for UI development, so apps written with React need additional libraries to be used. For instance, Redux, React Router, or Helmet optimize the processes of state management, routing, and interaction with the API. Such functions as data binding, component-based routing, project generation, form validation, or dependency injection require additional modules or libraries to be installed.</p>
<h3 id="heading-angular-2">Angular</h3>
<p>Angular is a full-fledged framework for software development, which usually does not require additional libraries. All the above-mentioned functions – data binding, component-based routing, project generation, form validation, and dependency injection – can be implemented with the means of Angular package.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/pFVFckO96mf60jYoDLsXA/df1d17d0c516c24b906feacc66a36b2d/open-book.png?h=300" alt="open-book" width="256" height="256" loading="lazy"></p>
<h2 id="heading-4-learning-curve">4. Learning Curve</h2>
<h3 id="heading-react-3">React</h3>
<p>React is minimalistic: no dependency injection, no classic templates, no overly complicated features. The framework will be quite simple to understand if you already know JavaScript well. </p>
<p>However, it takes quite some time to learn how to set up a project because there is no predefined project structure. You also need to learn the Redux library, which is used in more than half of React applications for state management. Constant framework updates also require additional effort from the developer. Furthermore, there are quite a lot of best practices in React, which you will need to learn to do things right.</p>
<h3 id="heading-angular-3">Angular</h3>
<p>Angular itself is a huge library, and learning all the concepts associated with it will take much more time than in the case of React. Angular is more complex to understand, there is a lot of unnecessary syntax, and component management is intricate. Some complicated features are embedded into the framework core, which means that the developer cannot avoid learning and using them. Moreover, there are a lot of ways of solving a single issue. </p>
<p>Although TypeScript closely resembles JavaScript, it also takes some time to learn. Since the framework is constantly updated, the developer needs to put some extra learning effort.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/3UoGpyTMCV8CGu1Z4kjpse/d32f947e97c9ca59b8d06ffa3cbcab5e/users__1_.png?h=300" alt="users (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-5-community">5. Community</h2>
<h3 id="heading-react-4">React</h3>
<p>React framework is one of the most popular JS frameworks worldwide, and the community supporting and developing it is huge.</p>
<p>Working with React, you have to be a continuous learner since the framework is often updated. While the community tries to go forward with the latest documentation as swiftly as possible, keeping up with all the changes is not that easy. Sometimes, there may be a lack of documentation, but the issue is often solved by the community support on thematic forums. </p>
<p>React is actively used by such companies as Facebook, Twitter, Netflix, Airbnb, PayPal, The New York Times, Yahoo, Walmart, Uber, and Microsoft.</p>
<h3 id="heading-angular-4">Angular</h3>
<p>Angular is less admired than React and faces a lot of skepticism, partially because of the unpopularity of Angular 1.0. Developers used to dismiss the framework as an overly complicated one as it required a lot of time to be spent learning. However, this framework has been developed by Google, which works in favor of Angular’s credibility. </p>
<p>Google provides the long-term support of the framework and constantly improves it. However, the updates are so fast that the documentation often falls behind. </p>
<p>Angular is used by such companies as McDonald’s, AT&amp;T, HBO, Apple, Forbes, Adobe, Nike, and Microsoft as well.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/42G1ot0FKIiLsLEss57U7C/afa7f06c694c2481af01923ade2c1f90/settings__1_.png?h=300" alt="settings (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-6-performance">6. Performance</h2>
<h3 id="heading-react-5">React</h3>
<p>React’s performance is greatly improved with the introduction of the virtual DOM. Since all virtual DOM trees are lightweight and built on server, the load on browser is reduced. Furthermore, since the data-binding process is unidirectional, bindings are not assigned watchers as in the case of Angular. Respectively, no additional workload is created.</p>
<h3 id="heading-angular-5">Angular</h3>
<p>Angular performs worse, especially in the case of complex and dynamic web apps. </p>
<p>The performance of Angular apps is negatively affected by bidirectional data-binding. Each binding is assigned a watcher to track changes, and each loop continues until all the watchers and associated values are checked. Because of this, the more bindings you have, the more watchers are created, and the more cumbersome the process becomes. </p>
<p>However, the most recent update of Angular has greatly improved its performance, and it does not lose to React anymore. Moreover, the size of an Angular application is slightly smaller than the size of a React app.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/LCPTXwhsTKQ4YasbWaJBg/3ecbcedd4687993fcca72086a16abc0f/menu__1_.png?h=300" alt="menu (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-7-language">7. Language</h2>
<h3 id="heading-react-6">React</h3>
<p>React is based on JavaScript ES6+ combined with JSX script. JSX is an extension for syntax, which makes a JavaScript code resemble that written in HTML. This makes the code easier to understand, and typos are easier to spot. For the JSX code to be compiled in a browser, React is augmented with Babel – a code translation tool.</p>
<h3 id="heading-angular-6">Angular</h3>
<p>Angular can use JavaScript or TypeScript, which is a superset of JS developed specifically for larger projects. TypeScript is more compact than JavaScript, the code is easier to navigate, and typos are easily spotted. Code refactoring process also becomes simpler and faster.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/34RQEkHFyn2MwWWbUxVwyx/d621c2de5692fb4b686b3702472676ab/layers__2_.png?h=300" alt="layers (2)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-8-app-structure">8. App Structure</h2>
<h3 id="heading-react-7">React</h3>
<p>The structure of React provides developers with the freedom to choose. There is no “the only right structure” for a React app. However, the necessity to design the app structure at the beginning of each project makes it more difficult and longer to start.</p>
<p>Besides, React offers only View layer, while Model and Controller are added with the usage of other libraries.</p>
<p>The architecture of a React app is component-based. The code is made of React components, which are rendered with React DOM library and directed in two ways: functional (with a function that returns JSX)...</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Hello</span>(<span class="hljs-params">props</span>)</span>{
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Hello {props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
}
</code></pre>
<p>...and class-based (with ES6 classes).</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hello</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {this.props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>;
}
</code></pre>
<h3 id="heading-angular-7">Angular</h3>
<p>The structure of Angular is fixed and complex, suitable for experienced developers.</p>
<p>Angular is based on three layers – Model, Controller, and View. An object responsible for the Model is initialized by the Controller and displayed with the View.</p>
<p>The application code consists of different Angular components, each being written in four separate files: a TypeScript to implement the component, an HTML file to define the view, a CSS file to define the stylistic features, and a special file for testing purposes. Links to these files are written in the app directive, which displays the structural logic of the app. Respectively, Angular components are also reusable.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
@Component({
<span class="hljs-attr">selector</span>: <span class="hljs-string">'my-app'</span>,
<span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'./app.component.html'</span>,
<span class="hljs-attr">styleUrls</span>: [<span class="hljs-string">'./app.component.css'</span>]
})
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppComponent</span> </span>{ }
</code></pre>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/1jnhjOLhvVfIJpf1JjAFlO/e14f81b5efdca246c0d2beab0e49c2f0/app__1_.png?h=300" alt="app (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-9-ui-components">9. UI Components</h2>
<h3 id="heading-react-8">React</h3>
<p>UI tools for React are developed by the community. There are a lot of free and paid UI components on the React portal. To use material design components in React, you would have to install an additional library – Material-UI Library &amp; Dependencies.</p>
<h3 id="heading-angular-8">Angular</h3>
<p>Angular has a built-in Material toolset, and it offers a variety of pre-built material design components. There are various buttons, layouts, indicators, pop-ups, and form controls. Because of this, UI configuration becomes simpler and faster.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/27zKcdqY3mOnzT5p8vrQbC/e987f42102e8875d0f7714d8db3288f8/file__1_.png?h=300" alt="file (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-10-directives">10. Directives</h2>
<h3 id="heading-react-9">React</h3>
<p>In React, templates and logic are explained in one place – at the end of the component. It allows the reader to quickly grasp the meaning of the code even if they do not know the syntax.</p>
<h3 id="heading-angular-9">Angular</h3>
<p>In Angular, each template is returned with an attribute – a directive of how the object has to be set. The syntax of Angular directives is complex and sophisticated, which makes it incomprehensible for a reader without any experience in working with this technology.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/4I2aqTboict17ysz5yGD4n/5a7fc19e15889f42b7561d34b18edae9/controls__1_.png?h=300" alt="controls (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-11-state-management">11. State Management</h2>
<h3 id="heading-react-10">React</h3>
<p>In React, each component has its own state. A React developer can create special components for holding the state of the entire application or a particular part of it. The major disadvantage here consists in the fact that the global state needs to be stored in multiple different parts of the app with data being manually passed around different component tree levels.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Clock</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
   <span class="hljs-built_in">this</span>.state = {<span class="hljs-attr">date</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()};
 }
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello world!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Now is {this.state.date.toLocaleTimeString()}.<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>To solve this problem, there is a special state management library – Redux. The idea of it is that the global state is represented as a single stateful object, which is altered in different parts of the app with the help of reducers – special Redux functions.</p>
<p>Another solution is offered by the state management library MobX. Unlike Redux with the global state stored in a single immutable stateful object, MobX offers storing only the minimal required state, while the rest of it can be derived.</p>
<h3 id="heading-angular-10">Angular</h3>
<p>In Angular, component data is stored in component properties. Parent components pass data through to children ones. State changes in some parts can be identified and recalculated, but in a large app, it can cause a multi-directional tree series of updates, which will be difficult to track. The features can be improved with the help of state management libraries, such as NgRx or RxJS , which would make sure that the data flow is unidirectional.</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroListComponent</span> <span class="hljs-title">implements</span> <span class="hljs-title">OnInit</span> </span>{
  <span class="hljs-attr">heroes</span>: Hero[];
  selectedHero: Hero;
  <span class="hljs-keyword">constructor</span>(private service: HeroService) { }
  ngOnInit() {
    <span class="hljs-built_in">this</span>.heroes = <span class="hljs-built_in">this</span>.service.getHeroes();
  }
  selectHero(hero: Hero) { <span class="hljs-built_in">this</span>.selectedHero = hero; }
}
</code></pre>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/1FrBshReFNJB1yDkeNVBPr/091aac131dccf0267061a450f76fb897/network__1_.png?h=300" alt="network (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-12-dependency-injection">12. Dependency Injection</h2>
<h3 id="heading-react-11">React</h3>
<p>React does not fully support dependency injection as it does not fully comply with the idea of functional programming and data immutability. Instead, it has a global state for all components.</p>
<h3 id="heading-angular-11">Angular</h3>
<p>The greatest advantage of Angular rests in the fact that, unlike React, it supports dependency injection. Therefore, Angular allows having different lifecycles for different stores.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Injectable } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { HEROES } <span class="hljs-keyword">from</span> <span class="hljs-string">'./mock-heroes'</span>;
@Injectable({
  <span class="hljs-comment">// we declare that this service should be created</span>
  <span class="hljs-comment">// by the root application injector.</span>
  <span class="hljs-attr">providedIn</span>: <span class="hljs-string">'root'</span>,
})
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroService</span> </span>{
  getHeroes() { <span class="hljs-keyword">return</span> HEROES; }
}
</code></pre>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/4FJFtvDwvmQdgsCUx5MYOO/b87ee7b57de5908ef20405e95b707959/unlink__1_.png?h=300" alt="unlink (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-13-data-binding">13. Data Binding</h2>
<h3 id="heading-react-12">React</h3>
<p>Data binding stands for the data synchronization process between Model and View. React should be augmented with Redux, which allows you to work with immutable data and makes data flow unidirectional. Unidirectional binding is predictable, which facilitates the debugging process.</p>
<h3 id="heading-angular-12">Angular</h3>
<p>Angular works with bidirectional data-binding and mutable data. While the advantages of mutable and immutable data are a matter of a heated discussion, it is definitely easier to work with bidirectional data-binding rather than with the unidirectional approach. At the same time, bidirectional data-binding negatively affects the performance since Angular automatically develops a watcher for each binding.</p>
<p>The ways of data-binding in Angular:</p>
<pre><code class="lang-js">{{expression}}   Interpolation
[target]=<span class="hljs-string">"expression"</span>    Property
bind-target=<span class="hljs-string">"expression"</span>    Attribute
</code></pre>
<pre><code class="lang-js">(target)=<span class="hljs-string">"statement"</span> Event
on-target=<span class="hljs-string">"statement"</span> Event
</code></pre>
<pre><code class="lang-js">[(target)]=<span class="hljs-string">"expression"</span> Two-way
bindon-target=<span class="hljs-string">"expression"</span> Two-way
</code></pre>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/2mOdA6gBK5MqwjRRdgm5Mm/d679f1314811e0ba93ae2b106ddbf5f1/shuffle__1_.png?h=300" alt="shuffle (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-14-change-rendering">14. Change Rendering</h2>
<h3 id="heading-react-13">React</h3>
<p>React uses a virtual Document Object Model (DOM), which enables easily implementing minor data changes in one element without updating the structure of the entire tree. The framework creates an in-memory cache of data structure, computes the changes, and efficiently updates the DOM displayed in the browser. This way, the entire page seems to be rendered on each change, whereas actually, the libraries render changed subcomponents only. </p>
<p>The React team is constantly improving Fiber – a mechanism aimed at boosting the productivity of change rendering.</p>
<h3 id="heading-angular-13">Angular</h3>
<p>Angular uses a real DOM, which updates the entire tree structure even when the changes have taken place in a single element. The real DOM is considered to be slower and less effective than the virtual DOM. </p>
<p>To compensate for this disadvantage, Angular uses change detection to identify components that need to be altered. Therefore, the real DOM on Angular performs as effectively as the virtual DOM on React.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/77lE2Lv97WHOpaxBVDPjfX/2b4e6f55becbd8aae05b983cb072f3c7/edit__1_.png?h=300" alt="edit (1)" width="256" height="256" loading="lazy"></p>
<h2 id="heading-15-tools">15. Tools</h2>
<h3 id="heading-react-14">React</h3>
<p>React is supported by multiple code editors. For instance, the code in React can be edited with <a target="_blank" href="https://www.sublimetext.com/">Sublime Text</a>, <a target="_blank" href="https://visualstudio.microsoft.com/">Visual Studio</a>, and <a target="_blank" href="https://atom.io/">Atom</a>. To bootstrap a project, you can use the Create React App (CLI) tool. In turn, server-side rendering is completed with the use of Next.js framework.</p>
<p>To test the entire app written in React, you would need multiple tools. For instance, <a target="_blank" href="https://github.com/airbnb/enzyme">Enzyme</a> for component testing, <a target="_blank" href="https://jestjs.io/">Jest</a> for testing JS code, React-unit for unit testing and so on. To debug the app in the development mode, you can use a browser extension React Dev Tools.</p>
<p>Another interesting tool is <a target="_blank" href="https://github.com/facebook/react-360">React 360</a>, which is a library used for creating AR and VR applications.</p>
<h3 id="heading-angular-14">Angular</h3>
<p>Similarly to React, Angular is supported by a variety of code editing tools. For example, you may work with such code editors as <a target="_blank" href="http://www.aptana.com/">Aptana</a>, <a target="_blank" href="https://www.sublimetext.com/">Sublime Text</a>, and <a target="_blank" href="https://visualstudio.microsoft.com/">Visual Studio</a>. A project can be promptly set up with <a target="_blank" href="https://cli.angular.io/">Angular CLI</a>. Server-side rendering is completed with the help of <a target="_blank" href="https://github.com/angular/universal">Angular Universal</a>.</p>
<p>Unlike React, Angular can be fully tested with a single tool. For the end-to-end testing in Angular, the platforms are <a target="_blank" href="https://jasmine.github.io/2.0/introduction.html">Jasmine</a>, <a target="_blank" href="http://www.protractortest.org/#/">Protractor</a>, and <a target="_blank" href="https://karma-runner.github.io/latest/index.html">Karma</a>. Another tool that debugs the app in the development mode is a browser extension <a target="_blank" href="https://augury.rangle.io/">Augury</a>.</p>
<h2 id="heading-to-wrap-up">To Wrap Up</h2>
<p><strong>Angular</strong> is a full-fledged mobile and web development framework. <strong>React</strong> is a framework only for UI development, which can be turned into a full-fledged solution with the help of additional libraries. </p>
<p>React seems simpler at first sight, and it takes less time to start working on a React project. However, that simplicity as the main advantage of React is neutralized because you have to learn to work with additional JavaScript frameworks and tools. </p>
<p>Angular itself is more complex and takes quite some time to master. Yet, it is a powerful tool that offers a holistic web development experience, and once you learn how to work with it, you reap the fruits. </p>
<p><strong>There is no better framework. Both are updated continuously to keep up with the competitor</strong>. For instance, while React was believed to win because of its virtual DOM, Angular equaled the score by implementing change detection. While Angular was considered to be winning because of being developed by such an authoritative company as Google, the immense devoted React community fully compensated for Google's reputation and made React similar to Angular. </p>
<p><strong>In the end, React vs Angular is all a matter of personal preference, a matter of skills and habits</strong>. As a beginner in programming, you would probably benefit more from starting with React. As an experienced developer, you just keep working with what you know better. </p>
<p>Do not forget to challenge yourself and start learning a new framework, React or Angular. As a Project Manager or a business owner outsourcing developers, you should talk to your web development team and together choose the framework that suits all of you best, whether it be Angular or React.</p>
<h2 id="heading-do-you-have-an-idea-for-either-an-angular-or-react-project">Do you have an idea for either an Angular or React project?</h2>
<p>My company KeenEthics is experienced in <a target="_blank" href="https://keenethics.com/tech-front-end-react">react development</a>, and we, also, provide outstanding <a target="_blank" href="https://keenethics.com/tech-front-end-angular">agularjs developers</a> for your projects. In case you need the following services, feel free to <a target="_blank" href="https://keenethics.com/contacts">get in touch</a><em>.</em></p>
<p>If you have enjoyed the article, you should definitely read another wonderful comparison of 2 JS frameworks: <a target="_blank" href="https://keenethics.com/blog/react-vs-svelte-how-to-build-messaging-components">React vs Svelte: How to Build Messaging Components</a>.</p>
<h2 id="heading-ps">P.S.</h2>
<p>I would like to say thank you to everyone who contributed to this article including Sergey Gornostaev and Volodya Andrushchak, full-stack software developers @ KeenEthics.</p>
<p>The original article posted on KeenEthics blog can be found here: <a target="_blank" href="https://keenethics.com/blog/angular-vs-react-what-to-choose-for-your-app">Angular vs React: Which One to Choose for Your App</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Angular 8 DOM Queries: ViewChild and ViewChildren Example ]]>
                </title>
                <description>
                    <![CDATA[ By Ahmed Bouchefra The @ViewChild and @ViewChildren decorators in Angular provide a way to access and manipulate DOM elements, directives and components. In this tutorial, we'll see an Angular 8 example of how to use the two decorators. You can use V... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/angular-8-dom-queries-viewchild-and-viewchildren-example/</link>
                <guid isPermaLink="false">66d45d5933b83c4378a517ae</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ angular8 ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 16 Jul 2019 11:28:09 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca16e740569d1a4ca4e8b.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ahmed Bouchefra</p>
<p>The <code>@ViewChild</code> and <code>@ViewChildren</code> decorators in Angular provide a way to access and manipulate DOM elements, directives and components. In this tutorial, we'll see an Angular 8 example of how to use the two decorators.</p>
<p>You can use <code>ViewChild</code> if you need to query one element from the DOM and <code>ViewChildren</code> for multiple elements.</p>
<p>We'll be using an online development IDE available from <a target="_blank" href="https://stackblitz.com/">https://stackblitz.com/</a> so you don't need to set up your local development machine for Angular at this point.</p>
<p>Head over to Stackblitz, sign in with your GitHub account and choose an Angular workspace:</p>
<p><img src="https://www.diigo.com/file/image/badcbccczobcaepapszdrpoesap/The+online+code+editor+for+web+apps.+Powered+by+Visual+Studio+Code.+-+StackBlitz.jpg" alt="Angular Stackblitz" width="600" height="400" loading="lazy"></p>
<p>You should be presented with an online IDE with an Angular 8 project</p>
<p><img src="https://www.diigo.com/file/image/badcbccczobcaepdsrzdrpoescs/angular-hcnsej+-+StackBlitz.jpg" alt="Angular 8 ViewChild Example" width="600" height="400" loading="lazy"></p>
<p>Our Angular project contains the usual <code>App</code> component and a child component called <code>HelloComponent</code> and defined in the <code>src/app/hello.component.ts</code> file with the following code:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, Input } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'hello'</span>,
  template: <span class="hljs-string">`&lt;h1&gt;Hello !&lt;/h1&gt;`</span>,
  styles: [<span class="hljs-string">`h1 { font-family: Lato; }`</span>]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> HelloComponent  {
  <span class="hljs-meta">@Input</span>() name: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p>The component accepts a <code>name</code> property and uses an inline template which simply displays the value passed via the name property from the parent component.</p>
<p>In the component decorator, we used <em>hello</em> as the selector for the component so in the the HTML template of the <code>App</code> component defined in the <code>src/app/app.component.html</code>file, we include the child component using the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">hello</span> <span class="hljs-attr">name</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">hello</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
  Start editing to see some magic happen :)
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>After running our Angular application the hello component is rendered and becomes part of the DOM so we can query it just like any normal DOM element.</p>
<h2 id="heading-whats-viewchild-in-angular"><strong>What's ViewChild in Angular?</strong></h2>
<p>ViewChild is a decorator that creates a view or DOM query. According to the <a target="_blank" href="https://angular.io/api/core/ViewChild#description">docs</a></p>
<p>A property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the view DOM. If the view DOM changes, and a new child matches the selector, the property is updated.</p>
<p>The decorator takes the following meta information:</p>
<ul>
<li><code>selector</code> - the selector of the element to query. This can be a directive type or a name.</li>
<li><code>read</code> - read a different token from the queried elements.</li>
<li><code>static</code> - This is new in Angular 8 and indicates whether or not to resolve query results before change detection runs.</li>
</ul>
<p><code>ViewChild</code> can take the following selectors:</p>
<ul>
<li>Classes with the <code>@Component</code> or <code>@Directive</code> decorators i.e components and directives,</li>
<li>Template reference variables,</li>
<li>Providers,</li>
<li><code>TemplateRef</code></li>
</ul>
<p>Now, let's go back to our <code>src/app/app.component.ts</code> file and let's see how we can query the child component using <code>ViewChild</code>. First, change the code accordingly:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, ViewChild, AfterViewInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-keyword">import</span> { HelloComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./hello.component'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'my-app'</span>,
  templateUrl: <span class="hljs-string">'./app.component.html'</span>,
  styleUrls: [ <span class="hljs-string">'./app.component.css'</span> ]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppComponent <span class="hljs-keyword">implements</span> AfterViewInit {
  name = <span class="hljs-string">'Angular'</span>;
  <span class="hljs-meta">@ViewChild</span>(HelloComponent, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) hello: HelloComponent;

  ngAfterViewInit() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello '</span>, <span class="hljs-built_in">this</span>.hello.name); 
  }
}
</code></pre>
<p>In the console, you should get <strong>Hello Angular</strong>:</p>
<p><img src="https://www.diigo.com/file/image/badcbccczobcaoaaaazdrpoobqo/angular-hcnsej+-+StackBlitz.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, let's explain the code.</p>
<p>First, we imported <code>HelloComponent</code> and <code>ViewChild</code> and <code>AfterViewInit</code> from the <code>@angular/core</code> package:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, ViewChild, AfterViewInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { HelloComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./hello.component'</span>;
</code></pre>
<p>Next, we create a query called <code>hello</code> that takes <code>HelloComponent</code> as the selector and has static equals to false:</p>
<pre><code class="lang-ts"><span class="hljs-meta">@ViewChild</span>(HelloComponent, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) hello: HelloComponent;
</code></pre>
<p>In Angular 8, timing for <code>ContentChild</code> and <code>ViewChild</code> needs to be specified explicitly.</p>
<p>See: ​<a target="_blank" href="https://angular.io/guide/static-query-migration#why-do-i-have-to-specify-static-false-isnt-that-the-default">Why do I have to specify <code>{static: false}</code>? Isn't that the default?</a></p>
<p>Next, in the <code>ngAfterViewInit()</code> life-cycle hook, we can use the query to access the DOM element for the hello component. In our example, we accessed the name property of the component, after it's mounted in the DOM, which contains the <em>Angular</em> string:</p>
<pre><code class="lang-ts">  ngAfterViewInit() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello '</span>, <span class="hljs-built_in">this</span>.hello.name); 
  }
</code></pre>
<p>We can access any properties and even methods from the queried component.</p>
<p><strong>Note</strong>: View queries are set before the <code>ngAfterViewInit</code> callback is called.</p>
<h2 id="heading-querying-standard-html-elements-with-template-references"><strong>Querying Standard HTML Elements with Template References</strong></h2>
<p>We can also query standard HTML elements using <code>ViewChild</code> and template reference variables. Let's go back to our <code>src/app/app.component.html</code> file and change it as follows:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">hello</span> <span class="hljs-attr">name</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">hello</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">p</span> #<span class="hljs-attr">pRef</span>&gt;</span>
  Start editing to see some magic happen :)
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>We simply added the <code>helloRef</code> template reference to our hello component. Now let's change our code to access the component using its reference. Go back to the <code>src/app/app.component.ts</code> file and change accordingly:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, ViewChild, AfterViewInit, ElementRef } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-keyword">import</span> { HelloComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./hello.component'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'my-app'</span>,
  templateUrl: <span class="hljs-string">'./app.component.html'</span>,
  styleUrls: [ <span class="hljs-string">'./app.component.css'</span> ]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppComponent <span class="hljs-keyword">implements</span> AfterViewInit {
  name = <span class="hljs-string">'Angular'</span>;
  <span class="hljs-meta">@ViewChild</span>(HelloComponent, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) hello: HelloComponent;

  <span class="hljs-meta">@ViewChild</span>(<span class="hljs-string">'pRef'</span>, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) pRef: ElementRef;

    ngAfterViewInit() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.pRef.nativeElement.innerHTML); 
    <span class="hljs-built_in">this</span>.pRef.nativeElement.innerHTML = <span class="hljs-string">"DOM updated successfully!!!"</span>; 
  }
}
</code></pre>
<p>We import <code>ElementRef</code> and we create a query configuration to access the <code>&lt;p&gt;</code> DOM element with the <code>#pRef</code> template reference as follows:</p>
<pre><code class="lang-ts">  <span class="hljs-meta">@ViewChild</span>(<span class="hljs-string">'pRef'</span>, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) pRef: ElementRef;
</code></pre>
<p>Next, in the <code>ngAfterViewInit()</code> method we can access and modify the native DOM element using the <code>nativeElement</code> object of <code>ElementRef</code>:</p>
<pre><code class="lang-ts">  <span class="hljs-meta">@ViewChild</span>(<span class="hljs-string">'pRef'</span>, {<span class="hljs-keyword">static</span>: <span class="hljs-literal">false</span>}) pRef: ElementRef;

    ngAfterViewInit() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.pRef.nativeElement.innerHTML);
    <span class="hljs-built_in">this</span>.pRef.nativeElement.innerHTML = <span class="hljs-string">"DOM updated successfully!!!"</span>; 
  }
</code></pre>
<p><img src="https://www.diigo.com/file/image/badcbccczobcaorpcazdrpoorep/angular-hcnsej+-+StackBlitz.jpg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is the live example from this <a target="_blank" href="https://stackblitz.com/edit/angular-8-viewchild-example">link</a>.</p>
<h2 id="heading-whats-viewchildren-in-angular"><strong>What's ViewChildren in Angular?</strong></h2>
<p><code>ViewChildren</code> is another property decorator which is used to query the DOM for multiple elements and return a <a target="_blank" href="https://angular.io/api/core/QueryList">QueryList</a>.</p>
<p>According to the <a target="_blank" href="https://angular.io/api/core/ViewChildren">docs</a>:</p>
<p>You can use ViewChildren to get the QueryList of elements or directives from the view DOM. Any time a child element is added, removed, or moved, the query list will be updated, and the changes observable of the query list will emit a new value.</p>
<p>Let's see an example.</p>
<p>Go to the <code>src/app/app.component.html</code> file and update it as follows:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">hello</span>  <span class="hljs-attr">name</span>=<span class="hljs-string">"Angular 6"</span>  &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">hello</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">hello</span>  <span class="hljs-attr">name</span>=<span class="hljs-string">"Angular 7"</span>  &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">hello</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">hello</span>  <span class="hljs-attr">name</span>=<span class="hljs-string">"Angular 8"</span>  &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">hello</span>&gt;</span>
</code></pre>
<p>We are simply diplsaying the hello component three times. Let's now query the DOM. Open the <code>src/app/app.component.ts</code> file and change it as follows:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { Component, ViewChildren, AfterViewInit, QueryList } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;

<span class="hljs-keyword">import</span> { HelloComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./hello.component'</span>;

<span class="hljs-meta">@Component</span>({
  selector: <span class="hljs-string">'my-app'</span>,
  templateUrl: <span class="hljs-string">'./app.component.html'</span>,
  styleUrls: [ <span class="hljs-string">'./app.component.css'</span> ]
})
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppComponent <span class="hljs-keyword">implements</span> AfterViewInit {
  name = <span class="hljs-string">'Angular'</span>;
  <span class="hljs-meta">@ViewChildren</span>(HelloComponent) hellos: QueryList&lt;<span class="hljs-built_in">any</span>&gt;;

  ngAfterViewInit() {

     <span class="hljs-built_in">this</span>.hellos.forEach(<span class="hljs-function"><span class="hljs-params">hello</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(hello));

  }
}
</code></pre>
<p>You should this output in the console:</p>
<p><img src="https://www.diigo.com/file/image/badcbccczobcapaodrzdrpoospd/angular-8-viewchildren-example+-+StackBlitz.jpg" alt="Angular 5 ViewChildren Example" width="600" height="400" loading="lazy"></p>
<p>Now, let's explain the code.</p>
<p>First we import <code>ViewChildren</code>, <code>AfterViewInit</code> and <code>QueryList</code> from <code>@angular/core</code> package.</p>
<p>Next, we create a query configuration for accessing multiple DOM elements:</p>
<pre><code class="lang-ts"><span class="hljs-meta">@ViewChildren</span>(HelloComponent) hellos: QueryList&lt;<span class="hljs-built_in">any</span>&gt;;
</code></pre>
<p><code>@ViewChildren</code> returns a <code>QueryList</code> which stores a list of items. When the state changes Angular will automatically update this query list.</p>
<p>Finally, in the <code>ngAfterViewInit()</code> method, we can iterate over the query list and log each DOM element:</p>
<pre><code class="lang-ts">  ngAfterViewInit() {
     <span class="hljs-built_in">this</span>.hellos.forEach(<span class="hljs-function"><span class="hljs-params">hello</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(hello));
  }
</code></pre>
<p>You can find the live example from this <a target="_blank" href="https://stackblitz.com/edit/angular-8-viewchildren-example">link</a>.</p>
<h2 id="heading-conclusions"><strong>Conclusions</strong></h2>
<p>In this tutorial, we've seen how we can access and modify the DOM in Angular 8 using <code>ViewChild</code> and <code>ViewChildren</code> decorators and a couple of other APIs like <code>QueryList</code>and <code>ngAfterViewInit()</code></p>
<p>This <a target="_blank" href="https://www.techiediaries.com/angular-dom-queries-viewchild/">post</a> was originally posted in <a target="_blank" href="https://www.techiediaries.com/">techiediaries</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
