Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display hidden (stars **) in place of password in angular2+

Tags:

angular

I have to display a Password column in a table in UI. However, i want the password to be hidden (denoted by -->**). So how do i do it.I display the data through *ngFor.

code -

component.html

   <tbody>
    <tr *ngFor="let data of result| orderBy: key : reverse|filter:filter| 
    paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
      <td>
        <a [hidden]= "accessIdHide" [routerLink]="['/eai/update- 
       inward-
   filter']"  (click)="forUpdateInward(data)" data-toggle="tooltip"  
   title="Update" >
          <span class="glyphicon glyphicon-plus-sign"></span>
        </a>{{data.SENDER_ID}}
      </td>
      <td>{{data.SERVICE_ID}}</td>
      <td>{{data.INWARD_IP}}</td>
      <td>{{data.INWARD_PORT}}</td>
      <td>{{data.USER_ID}}</td>
      <td>{{data.PASSWORD}}</td>
    </tr>
    </tbody>

component.ts

 export class InwardFilterMasterComponent implements OnInit {

 ngOnInit() {

  this.viewData();
  }


  viewData() {

   //Get the data from the database via http request

   this.loading = true;
   var url = config.url;
   var port = config.port;
   this.http.post("http://....) })
  .map(result => this.result = result.json())
  .subscribe((res: Response) => {
    this.loading = false;
    console.log("XXXXXXXXXXXX", res);
    });
   console.log("INSIDE COMPONENT");

  }
 }
like image 216
Techdive Avatar asked Oct 17 '25 16:10

Techdive


3 Answers

You can create a pipe e.g. password and apply that on your value. The benefit of the pipe is that you'll be able to use it in your entire app and a change in single file will be reflected in all the places where the pipe is used.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'password'
})
export class PasswordPipe implements PipeTransform {

  transform(value: string, replaceChar?: string): any {
    if (value === undefined) {
      return value;
    }
    // Replace with the specified character
    if (replaceChar) {
      return replaceChar.repeat(value.length);
    }
    // Replace value with asterisks
    return '*'.repeat(value.length);
  }
}

Use it on your object property like this:

<td>{{data.PASSWORD | password}}</td>

You can even use the pipe to specify your own replace character instead of "*" e.g.

<td>{{data.PASSWORD | password:'+'}}</td>

StackBlitz Demo

like image 103
Faisal Avatar answered Oct 20 '25 07:10

Faisal


You can create a pipe e.g. password and apply that on your value. The benefit of the pipe is that you'll be able to use it in your entire app and a change in single file will be reflected in all the places where the pipe is used.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'password'
})
export class PasswordPipe implements PipeTransform {

  transform(value: string, replaceChar?: string): any {
    if (value === undefined) {
      return value;
    }
    // Replace with the specified character
    if (replaceChar) {
      return replaceChar.repeat(value.length);
    }
    // Replace value with asterisks
    return '*'.repeat(value.length);
  }
}

Use it on your object property like this:

<td>{{data.PASSWORD | password}}</td>

You can even use the pipe to specify your own replace character instead of "*" e.g.

<td>{{data.PASSWORD | password:'+'}}</td>

StackBlitz Demo

like image 44
Faisal Avatar answered Oct 20 '25 08:10

Faisal


There is no point in displaying password if you want to put stars instead (I guess there is no point in displaying password at all). If you don't care about number of stars depending on password length you can display plain text '******'. If you care about number of stars you can display result of method hashPassword(data.PASSWORD) and in your ts file declare it as:

  hashPassword(password: string){
    return "*".repeat(password.length)
  }
like image 41
K. Stefaniak Avatar answered Oct 20 '25 08:10

K. Stefaniak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!