Home > Software design >  Im getting error while fetching data in angular
Im getting error while fetching data in angular

Time:01-31

TS2345: Argument of type (id: number; name: string; price: mumberg Storage: number; ) is not assignable to parameter of type 'null'.

<button  (click)-"openForm(mobile)">Edit</button>

app.component.html

<tr *ngFor="let mobile of mobiles">
   <td>{{ mobile.id }}</td>
   <td>{{ mobile.name}}</td>
   <td>{{ mobile.price }}</td> <td>{{ mobile.ram }}</td>
   <td>((mobile.Storage }}</td> 
<td>

<button  (click)="openForm (mobile)">Edit</

App.component.ts

If(data){
  this.mobileId=data.id;
  this.mobileName=data.name;
  this.mobileprice=data.price:
  this.mobileRam=data.ram;
  this.mobileStorage=data.Storage;
}

CodePudding user response:

You have to check the parameter type of the function and make ensure that is in the correct type. If it is supposed to be an object, ensure that the properties of the object match the expected properties in the function's definition you can also try to define the type of data as data : any.

CodePudding user response:

The error message is saying that the argument provided to the function openForm is not assignable to the parameter, which is expected to be of type null.

In the HTML code, there is a typo in the button's click event. It has a hyphen ((click)-) instead of an equal sign ((click)). Also, the closing tag for the button is missing the > character.

In the TypeScript code, there is a syntax error in the if statement. There is a colon (:) at the end of the line this.mobileprice=data.price:, which is not valid syntax.

Here's the corrected code:

app.component.html:

<button  (click)="openForm(mobile)">Edit</button>

app.component.ts:

if(data){
  this.mobileId=data.id;
  this.mobileName=data.name;
  this.mobileprice=data.price;
  this.mobileRam=data.ram;
  this.mobileStorage=data.Storage;
}
  • Related