1

Hi here i have a class as

export class Employeemodel {
    constructor() {
        this.name = "";
        this.gender = 0;
    }
    name: string;
    gender: number;
}

When i try to Bind data to This Class as

// EmployeeService.ts

GetData<T>(pagenum:number) {
    return this
        .http
        .get<Employeemodel[]>(this.apiUrl+'GetPagination/'+pagenum)
        .pipe(tap(data => {
            
        }));
}
// EmployeeComponent.ts
 
GetPagination(PageNo: number) {
    this._oservices
        .GetData(PageNo)
        .subscribe((result: Employeemodel[]) => {
            this.dataSource = new MatTableDataSource(result.otestgrid);   
        });
}

result.otestgrid Getting Error as Property 'otestgrid' does not exist on type 'Employeemodel[]'.

My Service call Giving Me output as

{
    "otestgrid": [
        {
            "name": "Dastageer",
            "gender": 1,
            "distName": "Hyderabad",
            "otestBindata": null
        },
}
4
  • can you please share this._oservices.GetData(PageNo) function in your question Commented May 13, 2021 at 7:37
  • 1
    @AshotAleqsanyan Just Now i Update my Question Please Go Commented May 13, 2021 at 7:40
  • 1
    your'e saying to typescript that the response if of type Employeemodel[], that's the reason you has an error. you can use subscribe((res:any)=> {..} or create an interface with the model of the response Commented May 13, 2021 at 7:41
  • @Eliseo Could you Please Help me how can i Create it from an Interface Commented May 13, 2021 at 7:42

2 Answers 2

1

your Employeemodel should be realized like this, Also I recommend in cammel case

export class EmployeeModel {
  "name": string;
  "gender": number;
  "distName"?: string;    // if it should be optional you can add `?`
  "otestBindata"?: unknown // unknown since I don't know what is the type, you can change
}

and actually, your response type is { otestgrid: EmployeeModel[] } if you didn't transform the data after getting the response.

so your function in the service should look like this

GetData(pagenum:number){
    return this.http
       .get<{ otestgrid: EmployeeModel[] }>(
          this.apiUrl+'GetPagination/'+pagenum
       ).pipe(tap(data=>{}));
}
Sign up to request clarification or add additional context in comments.

Comments

0

an interface it's only write

public interface DataResponse{
  otestgrid: EmployedData[]
}
public interface EmployedData{
{
 name:string;
 gender:number;
 distName:string;
 otestBindata": any;
}

See that, if we are not thinking about create a method, it's better use interface that class.

Remember that Angular "transpile" typescript (convert typescript to javascript) so the only that we are making using "typed variables" are help us to write code. Some like

this.http.get<{ otestgrid: EmployeeModel[] }>(...)

not sure that we received a response of type indicate, and don't transform the response. The response will be the response to the call -imagine you miss and indicate another call, Angular can not take account this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.