Author: therichpost

  • 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

  • WordPress trick to check term id has parent term or child term

    WordPress trick to check term id has parent term or child term

    Hello to all, welcome to therichpost.com. In this post, I will tell you, WordPress trick to check term id has parent term or child term.

    Post Working:

    In this post, on archive page, with my code, I am checking woocommerce category has parent category or child category.

    Here is the working code and you need add this into your theme’s archive.php page:

    <?php 
    $obj = get_queried_object();
    $term_id = $obj->term_id; 
    
    $child_term = get_term( $term_id, 'product_cat' );
    $parent_term = get_term( $child_term->parent, 'product_cat' );
    if($parent_term->term_id){
     // Parent category code
    }
    
    if($child_term->term_id){
     // Child category code
    }
    
    ?>

     

    If you have any query related to this post then please let me know.

    Jassa

    Thank you

  • Laravel 6 change user password complete working code

    Laravel 6 change user password complete working code

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Laravel 6 change user password complete working code.

    laravel change user password
    laravel change user password

    Post Working:

    In this post, I am doing, login user can change his/her password and I am doing this in Laravel 6. I will share, blade, route and controller code in this post.

    Here is the working code snippet and please use carefully:

    1. Here is the blade template file code:

    @if(session()->has("message"))
       <div class="alert alert-success">
       <p> {{session("message")}} </p>
       </div>
    @endif
    @foreach ($errors->all() as $error)
      <div class="alert alert-danger">
      <p>{{ $error }}</p>
      </div>
    @endforeach
    <form action="{{ URL::to('/change-password') }}" method="post">
      {{ csrf_field() }}
      <div class="form-group">
          <label for="image"><strong>Change Password:</strong></label>
          <input type="password" class="form-control" id="password" placeholder="New Password" name="password" required>
          <input type="password" name="objectId" value="{{ encrypt(Auth::user()->id) }}" style="display: none;">
      </div>
      <button type="submit" class="btn btn-warning">Submit</button>
    </form>

    2. Here is the route code:

    Route::post('/change-password','HomeController@changePassword');

    3. Here is the controller code:

    ...
    use Validator;
    ...
    public function changePassword(Request $request)
        {           
          $validator = Validator::make($request->all(), [
    
          'password' => 'required|max:255'
          ]);
    
          if ($validator->fails()) {
          return redirect()->back()
          ->withErrors($validator)
          ->withInput();
                }  
    
                $reset_password = DB::table('users')->where('id',decrypt(Input::get('objectId')))->update(['password' => bcrypt(Input::get('password'))]);    
                return Redirect::to('home')->with("message","Successful!! Password has been changed");
        }

     

     

    This is it, If you have any query then please do comment below.

    Jassa

    Thank you

  • Angular Laravel crud part 1

    Angular Laravel crud part 1

    Hello to all, welcome to therichpost.com. In this post, I will do Angular Laravel crud part 1.

    angular laravel crud part 1
    angular laravel crud part 1

    Post Working

    In this post, I will tell you Angular Laravel crud part 1. In this, I will do basic angular and laravel setup. I will use angular 8 and laravel 6. I will also do angular and laravel api integration to get and set data.

    Here is the code snippet and please use carefully:

    1. Here are the basics commands to install angular 8 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. After done with above, you need to run below commands to set bootstrap environment into your angular 8 application:

    Mostly I used bootstrap because of good looks to my application.

    npm install --save bootstrap

     

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

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

     

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

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { ReactiveFormsModule } from '@angular/forms';
    import { HttpClientModule } from '@angular/common/http';
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        BrowserAnimationsModule,
      ReactiveFormsModule,
      HttpClientModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

     

    Now come to laravel setup after then we can move forward to data fetching :

    1. Here is the post link from where you can have the basic laravel 6 setup on your machine:

    Laravel 6 basic setup

    Laravel auth setup

    Here you can see complete Angular Laravel CRUD working example:

    After the basics Angular and laravel setup, in my next post, I will move forward to crud operations.

    Jassa

    Thank you

  • Angular phone number validation working example

    Angular phone number validation working example

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular phone number validation working example.

    Guy’s we can use this post code snippet in Angular 12 for phone number validation.

    Angular phone number validation working example
    Angular phone number validation working example

    Post Working:

    In this post, I am doing reactive form validation. I am applying 10 digits number validation and only numbers will be enter into text input box.

    Guys Angular 12 came and if you are new in Angular 12 then please check below links:

    1. Angular 12 Tutorials
    2. Angular 12 Free Templates

    Here is the complete working code snippets and please follow carefully:

    1. Here are the basics commands to install angular 8 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. After done with above, you need to run below commands to set bootstrap environment into your angular 8 application:

    Mostly I used bootstrap because of good looks to my application.

    npm install --save bootstrap

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

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

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

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import { ReactiveFormsModule } from '@angular/forms';
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        ReactiveFormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

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

    import { Component } from '@angular/core';
    import { FormBuilder, FormGroup, Validators } from '@angular/forms';
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      registerForm: FormGroup;
      submitted = false;
      constructor(private formBuilder: FormBuilder) { }
    
      //only number will be add
      keyPress(event: any) {
        const pattern = /[0-9\+\-\ ]/;
    
        let inputChar = String.fromCharCode(event.charCode);
        if (event.keyCode != 8 && !pattern.test(inputChar)) {
          event.preventDefault();
        }
      }
    
      ngOnInit() {
        this.registerForm = this.formBuilder.group({
           phonenumber: ['', [ Validators.required,
            Validators.pattern("^[0-9]*$"),
            Validators.minLength(10), Validators.maxLength(10)]]
        });
    }
    // convenience getter for easy access to form fields
    get f() { return this.registerForm.controls; }
    onSubmit() {
        this.submitted = true;
        // stop here if form is invalid
        if (this.registerForm.invalid) {
            return;
        }
       
    }
      
    }

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

    <form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
    
    <div class="col-md-4">
                        <div class="form-group">
                            <label for="">YOUR PHONE NUMBER </label>
                            <input (keypress)="keyPress($event)" required type="text" formControlName="phonenumber" class="form-control" placeholder="Enter Your phone Number" [ngClass]="{ 'is-invalid': submitted && f.phonenumber.errors }">
                            <div *ngIf="submitted && f.phonenumber.errors" class="invalid-feedback">
                            <div *ngIf="f.phonenumber.errors.required">Phone number is required</div>
                            <div *ngIf="f.phonenumber.errors.pattern || f.phonenumber.errors.maxlength || f.phonenumber.errors.minlength">Phone number must be at least 10 numbers</div>
                        </div>
                        </div>
                    </div>
    
    <input type="submit" class="mw-ui-btn" value="Submit">
    </form>

    In the end, don’t forgot to run ng serve command. If you have any query then do comment below.

    Jassa

    Thank you.

  • Angular Laravel Starter

    Angular Laravel Starter

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular Laravel Starter.

    This post will be starting point of Angular + Laravel tutorials in multiple topics.

    Here are the Angular + Laravel tutorials list and also more tutorials will come soon:

    1. Angular 8 with laravel 6 backend working example
    2. Here is the Angular 8 Laravel 6 curd working example:

    If you have any query or you have any topic to cover then please let me know.

    Jassa

    Thank you

  • Angular 8 Child Routing Working Example

    Angular 8 Child Routing Working Example

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 8 Child Routing Working Example.

    If you are new then you can check my old posts related to Angular 8.

    Angular 8 Child Routing Working Example
    Angular 8 Child Routing Working Example

    Post Working

    In this post, I am showing, how child routes work. I have used Bootstrap 4 for navigation. In this post, you will see, how child routing and child component data will be shown and how child and parent will connect.

    Here are the working code snippets and please follow carefully:

    1. Very first, you need to run common below commands to add Angular 8 project on your machine:

    $ npm install -g @angular/cli 
    
    $ ng new angularselect2 //Install Angular Project
    
    $ cd angularselect2 // Go to project folder
    
    $ ng serve //Run Project
    
    http://localhost:4200/ //Run On local server

     

    2. Now you need to run below commands to add bootstrap 4 and jquery into your Angular application:

    npm install ngx-bootstrap bootstrap --save
    
    npm install jquery --save

     

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

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

     

    4. Now run below commands into your terminal to add components and child components into your angular 8 application:

    ng g component aboutus
    
    ng g component users
    
    //child components
    
    ng g component users/newuser
    
    ng g component users/edituser

     

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

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    import { AboutusComponent } from './aboutus/aboutus.component';
    import { UsersComponent } from './users/users.component';
    import { NewuserComponent } from './users/newuser/newuser.component';
    import { EdituserComponent } from './users/edituser/edituser.component';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { Routes, RouterModule } from '@angular/router';
    
    const appRoutes: Routes = [
      { path: 'aboutus', component: AboutusComponent },
      { path: 'users', component: UsersComponent,
        children: [
          {
            path: 'newuser',
            component: NewuserComponent
          },
          {
            path: 'edituser',
            component: EdituserComponent
          }
        ]
      },
    ];
    
    
    @NgModule({
      declarations: [
        AppComponent,
        AboutusComponent,
        UsersComponent,
        NewuserComponent,
        EdituserComponent
      ],
      imports: [
        BrowserModule,
        BrowserAnimationsModule, 
        RouterModule.forRoot(
          appRoutes,
          { enableTracing: true } // <-- debugging purposes only
        )
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

     

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

    <div class="jumbotron text-center">
      <h1>Angular 8 Child Routing Working Example</h1>
    </div>
    <nav class="navbar nav navbar-expand-sm bg-light">
      <ul class="nav nav-pills">
      <li class="nav-item"><a class="nav-link" routerLink="/aboutus">About Us</a></li>
      
      
      <li class="dropdown">
            <a class="dropdown-toggle nav-link" data-toggle="dropdown" routerLink="/users">Users
            <span class="caret"></span></a>
            <div class="dropdown-menu">
              <a class="dropdown-item"  routerLink="/users/newuser">Add User</a>
              <a class="dropdown-item"  routerLink="/users/edituser">Edit User</a>
            </div>
          </li>
    
      </ul>
    </nav>
    <br>
    
    <div class="container-fluid">
    <router-outlet></router-outlet>
    
    </div>

     

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

    <p>
      users works!
    </p>
    <router-outlet></router-outlet>

     

    In the end, don’t forgot to run ng serve command. If you have any query then do comment below.

    Jassa

    Thank you.

  • Angular material data table with custom button click event functionality

    Angular material data table with custom button click event functionality

    Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular material data table with custom button click event functionality.

    Guys is the new working demo in Angular 17:

    Please check the first part of this post : Angular material data table working example

    Angular material data table with custom button click event functionality
    Angular Material Datatable with custom button(Get Details)
    Angular material data table with custom button click event functionality open alert with row data
    Open Alert with row data after get details button click an
    Angular Material Datatable

    Post Working

    In this post, I am working with material angular datatable with custom button and when I will click on that custom button then I will get popup alert with dynamic row data.

    Guy’s Angular 17 came and if you are new in Angular 12 then please check below links:

    1. Angular 17 Tutorials
    2. Angular Free Templates

    Here is the working code snippet and please follow carefully:

    1. Here are the basics commands to install angular 11 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. After done with above, you need to run below command to add @angular/material into your angular 8 application:

    ng add @angular/material

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

    ...
    import {  MatTableModule } from '@angular/material/table';
    import {  MatPaginatorModule } from '@angular/material/paginator';
    import {  MatButtonModule } from '@angular/material/button'
    
    
    @NgModule({
     ...
      imports: [
        ...
        MatPaginatorModule,
        MatTableModule,
        MatButtonModule
       
      ],
    

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

    import { Component, OnInit, ViewChild } from '@angular/core';
    import {MatPaginator} from '@angular/material/paginator';
    import {MatTableDataSource} from '@angular/material/table';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', "getdetails"];
      dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
    
      @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
    
      ngOnInit() {
        this.dataSource.paginator = this.paginator;
      }
      getRecord(name)
      {
        alert(name);
      }
    }
    export interface PeriodicElement {
      name: string;
      position: number;
      weight: number;
      symbol: string;
    }
    const ELEMENT_DATA: PeriodicElement[] = [
      {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
      {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
      {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
      {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
      {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
      {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
      {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
      {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
      {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
      {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
      {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
      {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
      {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
      {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
      {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
      {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
      {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
      {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
      {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
    ];

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

    <div class="mat-elevation-z8">
      <table mat-table [dataSource]="dataSource">
    
        <!-- Position Column -->
        <ng-container matColumnDef="position">
          <th mat-header-cell *matHeaderCellDef> No. </th>
          <td mat-cell *matCellDef="let element"> {{element.position}} </td>
        </ng-container>
    
        <!-- Name Column -->
        <ng-container matColumnDef="name">
          <th mat-header-cell *matHeaderCellDef> Name </th>
          <td mat-cell *matCellDef="let element"> {{element.name}} </td>
        </ng-container>
    
        <!-- Weight Column -->
        <ng-container matColumnDef="weight">
          <th mat-header-cell *matHeaderCellDef> Weight </th>
          <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
        </ng-container>
    
        <!-- Symbol Column -->
        <ng-container matColumnDef="symbol">
          <th mat-header-cell *matHeaderCellDef> Symbol </th>
          <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
        </ng-container>
    
        <!-- Get Details -->
        <ng-container matColumnDef="getdetails">
          <th mat-header-cell *matHeaderCellDef> Details </th>
          <td mat-cell *matCellDef="let element"> <button mat-raised-button color="primary" (click)="getRecord(element.name)">Get Details</button> </td>
        </ng-container>
    
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>
    
      <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
    </div>
    

    6. Now you need to add below code into src/app/app.component.css file:

    table {width: 100%;}

    In the end, don’t forgot to run ng serve command. If you have any query then do comment below.

    Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding please watch the above video.

    I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

    Jassa

    Thank you.