explain pipes - amresh087/newronaRepos GitHub Wiki

  1. What are pipes?

    Pipes are simple functions that use template expressions to accept data as input and transform it into a desired output. For example, let us take a pipe to transform a component's birthday property into a human-friendly date using date pipe.

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-birthday',
      template: `<p>Birthday is {{ birthday | date }}</p>`
    })
    export class BirthdayComponent {
      birthday = new Date(1987, 6, 18); // June 18, 1987
    }
  2. What is a parameterized pipe?

    A pipe can accept any number of optional parameters to fine-tune its output. The parameterized pipe can be created by declaring the pipe name with a colon ( : ) and then the parameter value. If the pipe accepts multiple parameters, separate the values with colons. Let's take a birthday example with a particular format(dd/MM/yyyy):

    import { Component } from '@angular/core';
    
        @Component({
          selector: 'app-birthday',
          template: `<p>Birthday is {{ birthday | date:'dd/MM/yyyy'}}</p>` // 18/06/1987
        })
        export class BirthdayComponent {
          birthday = new Date(1987, 6, 18);
        }

    Note: The parameter value can be any valid template expression, such as a string literal or a component property.

  3. How do you chain pipes?

    You can chain pipes together in potentially useful combinations as per the needs. Let's take a birthday property which uses date pipe(along with parameter) and uppercase pipes as below

    import { Component } from '@angular/core';
    
            @Component({
              selector: 'app-birthday',
              template: `<p>Birthday is {{  birthday | date:'fullDate' | uppercase}} </p>` // THURSDAY, JUNE 18, 1987
            })
            export class BirthdayComponent {
              birthday = new Date(1987, 6, 18);
            }
  4. What is a custom pipe?

    Apart from built-in pipes, you can write your own custom pipe with the below key characteristics:

    1. A pipe is a class decorated with pipe metadata @Pipe decorator, which you import from the core Angular library For example,
          @Pipe({name: 'myCustomPipe'})
    2. The pipe class implements the PipeTransform interface's transform method that accepts an input value followed by optional parameters and returns the transformed value. The structure of PipeTransform would be as below,
      interface PipeTransform {
        transform(value: any, ...args: any[]): any
      }
    3. The @Pipe decorator allows you to define the pipe name that you'll use within template expressions. It must be a valid JavaScript identifier.
      template: `{{someInputValue | myCustomPipe: someOtherValue}}`
  5. Give an example of custom pipe?

    You can create custom reusable pipes for the transformation of existing value. For example, let us create a custom pipe for finding file size based on an extension,

      import { Pipe, PipeTransform } from '@angular/core';
    
      @Pipe({name: 'customFileSizePipe'})
      export class FileSizePipe implements PipeTransform {
        transform(size: number, extension: string = 'MB'): string {
          return (size / (1024 * 1024)).toFixed(2) + extension;
        }
      }

    Now you can use the above pipe in template expression as below,

       template: `
          <h2>Find the size of a file</h2>
          <p>Size: {{288966 | customFileSizePipe: 'GB'}}</p>
        `
  6. What is the difference between pure and impure pipe?

    A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe. For example, any changes to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object). An impure pipe is called for every change detection cycle no matter whether the value or parameters changes. i.e, An impure pipe is called often, as often as every keystroke or mouse-move.

  7. What is a bootstrapping module?

    Every application has at least one Angular module, the root module that you bootstrap to launch the application is called as bootstrapping module. It is commonly known as AppModule. The default structure of AppModule generated by AngularCLI would be as follows:

        import { BrowserModule } from '@angular/platform-browser';
        import { NgModule } from '@angular/core';
        import { FormsModule } from '@angular/forms';
        import { HttpClientModule } from '@angular/common/http';
    
        import { AppComponent } from './app.component';
    
        /* the AppModule class with the @NgModule decorator */
        @NgModule({
          declarations: [
            AppComponent
          ],
          imports: [
            BrowserModule,
            FormsModule,
            HttpClientModule
          ],
          providers: [],
          bootstrap: [AppComponent]
        })
        export class AppModule { }
⚠️ **GitHub.com Fallback** ⚠️