Home > Back-end >  Optional id in Angular interface provides argument of type number undefined is not assignable to par
Optional id in Angular interface provides argument of type number undefined is not assignable to par

Time:10-01

My interface has an optional Id:

export interface UserAccount{
    // User infos
    id?: number;
    firstName: string;
    lastName: string;
    mail: string;
    genderId: number;
    gender?: Gender;
    password: string;
    userName: string;
    login: string;
    useraccountTypeId: number;
    userAccountType?: UserAccountType;
    name?: string;
    commercialmail?: string;
    phone?: string;
    vatnumber?: string;
    truckId?: number;
    truck?: Truck;
}

This interface is used to create and update a user profile. My problem is that when I need to use the Id in the html part in a method I have the following message : argument of type number undefined is not assignable to parameter of type number.

What is the best practice to avoid this problem? Thanks for your help.

CodePudding user response:

you can use ngIf directive in your template

example:

<ng-container *ngIf="id">
// will render only if id is not (undefined,null,false and '')
</ng-container>

CodePudding user response:

Thanks to hritik chokker, I used this solution :

<ng-container *ngIf="u.id">
  <button type="button" id="details" class="btn btn-details" (click)="getUserDetails(u.id)">Details</button>    
</ng-container>

I am hoping this can help.
  • Related