Category: Angular7

  • How to make array push or empty in Angular 9 when getting data from API?

    How to make array push or empty in Angular 9 when getting data from API?

    Hello to all, welcome again on therichpost.com. In this post, I will tell you, How to make array push or empty in Angular 9 when getting data from API?

    Post Working:

    In this post, I am showing with video example that, how I am pushing the data to an array and also make that array empty during getting updated data.

    Here is working code and please use or check carefully:

    1. Here is my app.component.ts file and below code will push api data to an array:

    ...
    data = []; //Declare array variable
    
      constructor(private http: HttpClient) {
        this.http.get('http://localhost/mypage.php').subscribe(data => {
      
        this.data.push(data); // Pushing data to declared array variable
        console.log(this.data);
       
        
        }, error => console.error(error));
      
      }
    ...

     

    2. Here is my app.component.ts file and below code will make array variable empty:

    refresh()
      {
       this.data.length=0; //Make array variable empty
       this.http.get('http://localhost/mypage.php').subscribe(data => {
       this.data.push(data); //Push updated data
       console.log(this.data);
       
        
        }, error => console.error(error));
      }

     

    if you have any query then please do comment below.

    Jassa

    Thank you

  • How to store global variables in Angular 9?

    How to store global variables in Angular 9?

    Hello to all, in this post, I will tell you, How to store global variables in Angular 9?

    Localstorage is use to store data with no expiration date. We will manually destroy it.

    In this post, I am creating and deleting the local storage data and it is easy to use.

    Here is the working Image just for testing:
    Best Practice for Localstorage in Angular

    In above picture, there are two buttons, Get item and Delete item:

    Get Item is set the value for variable.

    Delete Item is remove the value from variable.

    Here is the working and tested code, you need to follow:

    1. First, you need to write below code into yours app.component.ts file:

    Here,  we will set the localstorage data and also delete it:

    import {Component} from '@angular/core';
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
    
      get dataV(): any {
        return localStorage.getItem('blog'); //Get Global Variable Value
      }
    
      delete()
      {
    
        localStorage.removeItem('blog'); //Delete Global Variable
      }
      get()
      {
    
        localStorage.setItem('blog', 'therichpost.com'); //Set Global Variable
      }
    }

     

    2. Second, you need write below code into your app.component.html file:

    In this file, we will make the buttons and get the localstorage data:

    <div class="jumbotron text-center">
      <h1>Angular LocalStorage
    </h1>
    </div>
    <div class="container">
    <button (click)="get()">Get Item</button><br><br>
    <button (click)="delete()">Delete Item</button>
    <h1>{{ dataV }}</h1>
    </div>

     

    localstoragedata

    If you have any query related to this post, then do comment below or ask questions.

    Jassa

    Thanks

  • How to login into Angular 9 application with Facebook?

    How to login into Angular 9 application with Facebook?

    Hello to all, welcome again on therichpost.com. In this post, I will tell you, How to login into Angular 9 application with Facebook?

    Here are the complete commands and code snippet and please follow carefully:

    1. Here are the basics commands to set angular 9 your system:

    npm install -g @angular/cli 
    
    ng new angularbootstrap //Create new Angular Project
    
    $ cd angularbootstrap // Go inside the Angular Project Folder
    
    ng serve --open // Run and Open the Angular Project
    
    http://localhost:4200/ // Working Angular Project Url

     

    2. Here is the command to install bootstrap 4 into your angular 9 applicatiopn:

    npm install --save bootstrap

    3. Here is the command to install Angular 9 Social login module:

    npm install --save angularx-social-login

     

    4. Here are the bootstrap 4 css and js path, you need to include into your angular.json file:

    "styles": [
            ...
            "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ...
             ],
    "scripts": [
           ...
          "node_modules/bootstrap/dist/js/bootstrap.min.js"
          ...
           ]

     

    5. Here is the html code, you need to add into your src\app\app.component.html file:

    div class="container">
      <div class="row">
        <div class="col-sm-12">
          <h4>About Me</h4>
         <h3>{{user.name}}</h3>
          <h5>Photo of me:</h5>
          <div class="fakeimg"><img src="{{user.photoUrl}}"></div>
         <button (click)="signInWithFB()" class="btn btn-primary">Login Facebook</button>
       </div>
      </div>
    </div>

     

     

    6. Here is the code, you need to add into your app.module.ts file:

    ...
    
    import { SocialLoginModule, AuthServiceConfig } from "angularx-social-login";
    import { FacebookLoginProvider } from "angularx-social-login";
    let config = new AuthServiceConfig([
      
      {
        id: FacebookLoginProvider.PROVIDER_ID,
        provider: new FacebookLoginProvider("YourFacebookAppID")
      }
    ]);
    
    export function provideConfig() {
      return config;
    }
    ...
    
    ...
     providers: [
          {
          provide: AuthServiceConfig,
          useFactory: provideConfig
          } 
      ],
    ...

     

     

    6. Here is the code, you need to add into your app.component.ts file:

    import { Component } from '@angular/core';
    import { AuthService } from "angularx-social-login";
    import { FacebookLoginProvider} from "angularx-social-login";
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'angularbootstrap';
      private user: any;
      private loggedIn: boolean;
      constructor(private authService: AuthService) { }
      //Logion
      signInWithFB(): void {
        this.authService.signIn(FacebookLoginProvider.PROVIDER_ID);
      
      } 
      // Logout Function
      signOut(): void {
        this.authService.signOut();
      }
      ngOnInit() {
        //Get User Data
        this.authService.authState.subscribe((user) => {
          this.user = user;
        
          this.loggedIn = (user != null);
        });
      }
    }
    

     

    This is it and please run ng serve command and check the output. If you have any kind of query then please do comment below.

    Jassa

    Thank you

  • How to make custom quantity selector into Angular 9 application?

    How to make custom quantity selector into Angular 9 application?

    Hello to all, welcome again on therichpost.com. In this post, I will tell you, How to make custom quantity selector into Angular 9 application?

    Here are the complete commands and code snippet and please follow carefully:

    1. Here are the basics commands to set angular 9 your system:

    npm install -g @angular/cli 
    
    ng new angularbootstrap //Create new Angular Project
    
    $ cd angularbootstrap // Go inside the Angular Project Folder
    
    ng serve --open // Run and Open the Angular Project
    
    http://localhost:4200/ // Working Angular Project Url

     

    2. Here is the command to install bootstrap 4 into your angular 9 applicatiopn:

    npm install --save bootstrap

     

    3. Here are the bootstrap 4 css and js path, you need to include into your angular.json file:

    "styles": [
            ...
            "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ...
             ],
    "scripts": [
           ...
          "node_modules/bootstrap/dist/js/bootstrap.min.js"
          ...
           ]

     

    4. Here is the html code, you need to add into your src\app\app.component.html file:

    <!-- Add icon library -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <div class="container" style="margin-top:30px">
      <div class="row">
        <div class="col-sm-4">
          <h5>Product Qunatity Button demo:</h5>
        </div>
        <div class="col-sm-8 mb-5">
      <div class="form-check-inline">
      <button class="btn btn-primary" (click)="plus()"><i class="fa fa-plus"></i></button>
      </div>
      <div class="form-check-inline">
      <input type="text" class="form-control" [(ngModel)]="inputnumber">
      </div>
      <div class="form-check-inline">
      <button class="btn btn-primary" (click)="minus()"><i class="fa fa-minus"></i></button>
      </div>
          
        </div>
      </div>
    </div>
    

     

    5. Here is the code, you need to add into your app.module.ts file:

    ...
    import { FormsModule } from '@angular/forms';
    ...
    
    imports: [
        ...
      FormsModule
        ...
      ],
    ...

     

    6. Here is the code, you need to add into your app.component.ts file:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'angularbootstrap';
      inputnumber = 0;
      
      plus()
      {
       this.inputnumber = this.inputnumber+1;
      }
      minus()
      {
        if(this.inputnumber != 0)
      {
       this.inputnumber = this.inputnumber-1;
      }
      
      }
    }
    

     

    This is it and please run ng serve command and check the output. If you have any kind of query then please do comment below.

    Jassa

    Thank you

  • Angular 9 Bootstrap 4 theme integration

    Angular 9 Bootstrap 4 theme integration

    Hello to all, welcome again on therichpost.com. In this post, I will tell you, Angular 9 Bootstrap 4 theme integration.

    Here are the complete commands and code snippet and please follow carefully:

    1. Here are the basics commands to set angular 9 your system:

    npm install -g @angular/cli 
    
    ng new angularbootstrap //Create new Angular Project
    
    $ cd angularbootstrap // Go inside the Angular Project Folder
    
    ng serve --open // Run and Open the Angular Project
    
    http://localhost:4200/ // Working Angular Project Url

    2. Here is the command to install bootstrap 4 into your angular 9 applicatiopn:

    npm install --save bootstrap

    3. Here are the commands to create header and footer components:

    ng g c header
    
    ng g c footer

    4. Here are the bootstrap 4 css and js path, you need to include into your angular.json file:

    "styles": [
            ...
            "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ...
             ],
    "scripts": [
           ...
          "node_modules/bootstrap/dist/js/bootstrap.min.js"
          ...
           ]

    5. Here is the html code, you need to add into your src\app\header\header.component.html file:

    <div class="jumbotron text-center" style="margin-bottom:0">
      <h1>My First Bootstrap 4 Page</h1>
      <p>Resize this responsive page to see the effect!</p> 
    </div>
    
    <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="collapsibleNavbar">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>    
        </ul>
      </div>  
    </nav>

    6. Here is the html code, you need to add into your src\app\footer\footer.component.html file:

    <div class="jumbotron text-center" style="margin-bottom:0">
      <p>Footer</p>
    </div>

    7. Here is the html code, you need to add into your src\app\app.component.html file:

    <app-header></app-header>
    <div class="container" style="margin-top:30px">
      <div class="row">
        <div class="col-sm-4">
          <h2>About Me</h2>
          <h5>Photo of me:</h5>
          <div class="fakeimg">Fake Image</div>
          <p>Some text about me in culpa qui officia deserunt mollit anim..</p>
          <h3>Some Links</h3>
          <p>Lorem ipsum dolor sit ame.</p>
          <ul class="nav nav-pills flex-column">
            <li class="nav-item">
              <a class="nav-link active" href="#">Active</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item">
              <a class="nav-link disabled" href="#">Disabled</a>
            </li>
          </ul>
          <hr class="d-sm-none">
        </div>
        <div class="col-sm-8">
          <h2>TITLE HEADING</h2>
          <h5>Title description, Dec 7, 2017</h5>
          <div class="fakeimg">Fake Image</div>
          <p>Some text..</p>
          <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
          <br>
          <h2>TITLE HEADING</h2>
          <h5>Title description, Sep 2, 2017</h5>
          <div class="fakeimg">Fake Image</div>
          <p>Some text..</p>
          <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
        </div>
      </div>
    </div>
    <app-footer></app-footer>

    This is it and please run ng serve command and check the output. If you have any kind of query then please do comment below.

    Jassa

    Thank you

  • How to make title dynamic in Angular 9?

    How to make title dynamic in Angular 9?

    Hello to all, welcome to therichpost.com. In this post, I will tell you, How to make title dynamic in Angular 9?

    Here are the basics commands and code and please follow carefully:

    1. Here are the basics commands to install angular 9 on your system:

    npm install -g @angular/cli 
    
    ng new angularpopup //Create new Angular Project
    
    cd angularpopup // Go inside the Angular Project Folder
    
    ng serve --open // Run and Open the Angular Project
    
    http://localhost:4200/ // Working Angular Project Url

    2. Here is the code for app.component.ts file:

    import {Title} from "@angular/platform-browser";
    ...
    export class AppComponent {
    ...
    
    constructor(private titleService:Title) {
       this.titleService.setTitle("Add Product");
      }
    
    ...
    
    }

    This is it and if you have any kind of query then please comment below.

    Jassa

    Thank you

  • Angular 8 input phone number with country code

    Angular 8 input phone number with country code

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 8 input phone number with country code.

    Guy’s this post code snippet will also work in angular latest version Angular 17 input type phone number with country code and country flags.

    Angular 8 input phone number with country code
    Angular 8 input phone number with country code
    Angular Input with Country Code Working Video

    Guys click here to see the updated version of this post for Angular 11+ versions.

    Post Working:

    In this post, I am showing input field with enter phone number with country code and country flags.

    Here is the working coding steps and please follow carefully:

    1. Here are the basics commands to install angular 17 on your system:

    npm install -g @angular/cli 
    
    ng new angularpopup //Create new Angular Project
    
    cd angularpopup // Go inside the Angular Project Folder
    
    ng serve --open // Run and Open the Angular Project
    
    http://localhost:4200/ // Working Angular Project Url

    2. Here is the below command you need to run into your terminal to add intl tel input into your angular 8 application:

    npm install ng2-tel-input intl-tel-input --save

    3. Now you need to add below code into your angular.json file:

    ...
    "styles": ["node_modules/intl-tel-input/build/css/intlTelInput.css"],
    "scripts": ["node_modules/intl-tel-input/build/js/intlTelInput.min.js"]
    ...

    4. Now you need to add below code into your src/app/app.module.ts file:

    ...
    import {Ng2TelInputModule} from 'ng2-tel-input';
    ...
    
    imports: [...  Ng2TelInputModule ...]

    5. Now you need to add below code into your src/app/app.component.html file:

    <input type="text" ng2TelInput />

    This is it guy’s and if you will have any kind of query related to this post then feel free to comment below.

    Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.

    Jassa

    Thank you

  • Angular playing API data

    Angular playing API data

    Hello to all, welcome to therichpost.com. In this post, I will tell you, how to playing with API data in Angular 7, Angular 8 and Angular 9.

    Post Working:

    In this post, I will show the code snippet in which I am getting data from laravel in json format and I am getting that json data in my angular 9 application with the help of HttpClient and then call that data into my angular html component.

    This will be helpful in Angular 7, Angular 8 and Angular 9.

    Here is the working code snippet and please use carefully:

    1. Here is the code for app.component.ts file:

    ...
    import { HttpClient} from '@angular/common/http';
    ...
    
    export class AppComponent implements OnInit {
      data: any;
      constructor( private http: HttpClient) { 
        // Example API DATA Format [{"id":22,"title":"Title 1"},{"id":23,"title":"Title 2"},{"id":24,"title":"Title 3"},{"id":25,"title":"Title 4"}]
        this.http.get('APIURL').subscribe(data => {
            this.data = data;
            }, error => console.error(error));
      }
    
    }

     

    2. Here is the code for app.component.html file:

    <ul>
     <li *ngFor="let item of data; let i = index" [attr.data-index]="i">
       {{item.title}}
     </li>
    </ul>

     

    This is it and I think this will be helpful and if you have any query then please let me know.

    Jassa

    Thank you

  • Angular 7,8,9 useful 5 hacks

    Angular 7,8,9 useful 5 hacks

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 7,8,9 useful 5 hacks.

    1. Limit string length in angular:

     <p class="card-text">{{item.description | slice:0:100}}...</p>
    // This will show first 100 words

    2. String replace in Angular:

    <p class="card-text">{{item.title.replace(' ','-')}}</p>
    // This will replace empty space with -

    3. String Lowercase in Angular:

    <p class="card-text">{{item.description | lowercase}}</p>

    4. Date Format in Angular:

    <div class="date">{{item.dateadded | date:'dd'}}<br>{{item.dateadded | date:'MMM'}}</div>
    // This will show the date in format what you want

    5. Looping in Angular:

     <ul>
       <li *ngFor="let tag of tags; let i = index" [attr.data-index]="i">
           <a href="#">{{tag}}</a>
       </li>
     </ul>
    // tags data is coming from angular component and number the tags data will generate same number of li

     

    I will share more in angular and if you have any query then please let me know.

    Jassa

    Thank you

  • Angular Latest Versions Loader Working Example

    Angular Latest Versions Loader Working Example

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular Latest Versions Loader Working Example.

    I am doing this in Angular7.2.4 version. This is very interesting post because I liked it very much.

    In this post, loader will show according to content load and this is the awesome thing.

    I am calling Angular loader before API data will load successfully and I am getting API data with Angular HTTP Client.



    Here is the working code, you need to follow carefully:


    1. Here is the code, you need to add into app.module.ts file:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    
    // we need this because we are calling API to get records
    import { HttpClientModule } from '@angular/common/http';
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        HttpClientModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

     

    2. You need to add below code into your app.component.ts file:

    import { Component } from '@angular/core';
    // HttpClient will call the API and get data
    import { HttpClient } from '@angular/common/http';
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title  = 'angularloader'; //Initiate loader
      loader = true;
      post   : any;
      constructor(private http: HttpClient) {
      this.http.get('https://project/posts').subscribe(data => {
      this.post = data;
      this.loader = false;
        }, error => console.error(error));
      }
    }

     

    3. You need to add below code into app.component.html file:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="loader" *ngIf="loader">
      <div class="bar"></div>
    </div>
    
    <div class="jumbotron text-center">
      <h1> Welcome to {{ title }}!</h1>
      <p>Resize this responsive page to see the effect!</p> 
    </div>
      
    <div class="container">
      <div class="row">
        <ul>
         <!-- Get The API Data -->
          <li *ngFor="let Posts of post">
            <span>{{ Posts.id }}</span>
            <h2>{{ Posts.title }}</h2>
            <p>{{ Posts.body }}</p>
          </li>
        </ul>
      </div>
    </div>
    
    </body>
    </html>

     

    4. You need to add the below code into app.component.css file to styling loader:

    @keyframes loader-animation {
         0% {
             left: -100%;
         }
         49% {
             left: 100%;
         }
         50% {
             left: 100%;
         }
         100% {
             left: -100%;
         }
     }
     .loader {
         height: 5px;
         width: 100%;
         overflow: hidden;
     }
     .loader .bar {
         position: relative;
         height: 5px;
         width: 100%;
         background-color: dodgerblue;
         animation-name: loader-animation;
         animation-duration: 3s;
         animation-iteration-count: infinite;
         animation-timing-function: ease-in-out;
     }

     

    This is it, if you have any query regarding this post, then please let me know.

    Jassa Jatt

    Thank you