简体   繁体   English

自定义api格式的返回列表问题

[英]problem with return list with custom api format

I return this list by this format from server : 我从服务器返回此格式的列表:

{data: Array(6), success: true, status: 0, message: "عملیات با موفقیت انجام شد"}

data: 数据:

data: Array(6)
         0: {name: "سیاسی", parentID: 0, categoryParent: null, categories: null, posts: null, …}
         1: {name: "ipouyoiyoi", parentID: 0, categoryParent: null, categories: null, posts: null, …}
         2: {name: "سیاسی", parentID: 0, categoryParent: null, categories: null, posts: null, …}
         3: {name: "ورزشی", parentID: 0, categoryParent: null, categories: null, posts: null, …}
         4: {name: "هنری", parentID: 0, categoryParent: null, categories: null, posts: null, …}
         5: {name: "گردشگری", parentID: 0, categoryParent: null, categories: null, posts: null, …}
message: "عملیات با موفقیت انجام شد"
status: 0
success: true

now i create a Generic Model: 现在我创建一个通用模型:

export interface GenericModel<T> {
 data:T;
 isSuccess:boolean;
 statusCode:number;
 message:string;
}

and this is my category model: 这是我的分类模型:

export interface CategoryModel {
id:number;
name:string;
parentId:number;
}

i create this field in component: 我在组件中创建此字段:

listCatModel:CategoryModel[];

and send a request to service with this , and i need to fill my property : 并发送请求服务,我需要填写我的财产:

  GetMainCat(){
this.categoryService.GetListItem(this.GetAllcatListUrl).subscribe(data=>{
  this.listCatModel=data
});
}

and in html i use this code: 并在HTML中我使用此代码:

 <option *ngFor="let item of listCatModel.data" selectedCat="cat.id" [value]="item.id">{{item.name}}</option>

now it show me this error: 现在它告诉我这个错误:

ERROR TypeError: Cannot read property 'data' of undefined 错误类型错误:无法读取未定义的属性“数据”

at Object.eval [as updateDirectives] (AddcategoriesComponent.html:15) 在Object.eval [as updateDirectives](AddcategoriesComponent.html:15)

at Object.debugUpdateDirectives [as updateDirectives] (core.js:23910) at Object.debugUpdateDirectives [as updateDirectives](core.js:23910)

at checkAndUpdateView (core.js:23306) 在checkAndUpdateView(core.js:23306)

What is the problem? 问题是什么? How can i solve this problem? 我怎么解决这个问题?

You make asynchronous request, and on the initialization time of your component listCatModel is undefined. 您进行异步请求,并且在组件的初始化时间未定义listCatModel You need to initialize it in component: 您需要在组件中初始化它:

public listCatModel: CategoryModel[] = [];

Moreover, your CategoryModel don't have data property and it's an array so you cant access it. 此外,您的CategoryModel没有data属性,它是一个数组,因此您无法访问它。 I think that you wanted to use your generic model here: 我想你想在这里使用你的通用模型:

public listCatModel: GenericModel<CategoryModel>[] = [];

Now, We need to fix *ngFor inside component.html 现在,我们需要在component.html中修复* ngFor

let item of listCatModel.data // typeof listCatModel is array - wrong

// this should be working
<option *ngFor="let item of listCatModel">
    {{ item.data.name }}
</option>

Try to initialize it in your component with 尝试使用在组件中初始化它

    listCatModel:CategoryModel[] = [];

When not initialized, your listCatModel will default to undefined . 未初始化时, listCatModel将默认为undefined

What Patryk Uszyński said. 什么PatrykUszyński说。

plus: 加:

listCatModel:CategoryModel[];

But the interface CategoryModel has no attribute data. 但是接口CategoryModel没有属性数据。
You probably want to use GenericModel? 你可能想使用GenericModel?

like so: 像这样:

listCatModel:GenericModel[] = [];

You are performing an async operation. 您正在执行异步操作。 Therefore before the subscribe callback is fired, your list is undefined . 因此,在触发subscribe回调之前,您的列表是undefined Instead of manually subscribing to the observable, let Angular do it for you using async pipe. 不要手动订阅observable,而是让Angular使用async管道为你做。 (Of course you need to call GetMainCat() before the view is inited) (当然你需要在视图被调用之前调用GetMainCat()

In component: 在组件中:

import {map} from 'rxjs/operators';
......

listCatModel: Observable<CategoryModel[]>;

GetMainCat(){
   this.listCatModel = this.categoryService.GetListItem(this.GetAllcatListUrl).pipe(map(response => response.data));
}

In template: 在模板中:

<option *ngFor="let item of (listCatModel | async)" selectedCat="cat.id" [value]="item.id">
    {{item.name}}
</option>

If you call GetMainCat() after the view is inited, you will still get undefined error. 如果在视图出现后调用GetMainCat() ,则仍会出现undefined错误。 To solve it, you need to check whether listCatModel is defined: 要解决此问题,您需要检查是否已定义listCatModel

<ng-container *ngIf="listCatModel">
    <option *ngFor="let item of (listCatModel | async)" selectedCat="cat.id" [value]="item.id">
        {{item.name}}
    </option>
<ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM