Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在尝试对可观察对象进行排序时,Angular 8 Error sort不是一个函数_Angular_Typescript_Sorting - Fatal编程技术网

在尝试对可观察对象进行排序时,Angular 8 Error sort不是一个函数

在尝试对可观察对象进行排序时,Angular 8 Error sort不是一个函数,angular,typescript,sorting,Angular,Typescript,Sorting,我试图使用http.get对从服务返回的可观察对象进行排序。在订阅之后,我尝试对服务和组件中的数据进行排序。但我总是得到错误排序不是一个函数。我是个新手,我一直在做这种事情。谢谢你的帮助。谢谢 JSON sample "data": [ { "codeValue": 2, "codeType": 145, "description": "ASSIMILATION" }, { "codeValue": 3,

我试图使用http.get对从服务返回的可观察对象进行排序。在订阅之后,我尝试对服务和组件中的数据进行排序。但我总是得到错误排序不是一个函数。我是个新手,我一直在做这种事情。谢谢你的帮助。谢谢

JSON sample

"data": [

    {
        "codeValue": 2,
        "codeType": 145,
        "description": "ASSIMILATION"
    },
    {
        "codeValue": 3,
        "codeType": 145,
        "description": "BANKRUPTCY"
    },
    {
        "codeValue": 4,
        "codeType": 145,
        "description": "BID TENDER"
    },

event.interface.ts

export interface IEventInterface {
codeValue: number;
codeType: number;
description: string;

}


Service.ts
getEventType():
{

    return this.http.get<IEventInterface[]>('/gpscaservices/v1/system- 
 descriptions?codeType=145&codeValue=');

}

Create.Component.ts

orderedEvent: any;
eventTypes: any;

this._dataService.getEventType().subscribe({
next: (response: IEventInterface[]) => {
            this.eventTypes = response  ;
            this.sortBy('description');
        }

sortBy(field: string ) {
               this.eventTypes.sort((a: any, b: any) =>{
        if (a[field] < b[field]) {
            return -1;
        } else if (a[field] > b[field]) {
            return 1;
        } else {
            return 0;
        }
    });

        this.orderedEvent = this.eventTypes;

Create.Component.html

nx-option *ngFor="let eventType of orderedEvent.data" 
[value]="eventType.codeValue">
{{ eventType.description }}
/nx-option>
JSON示例
“数据”:[
{
“代码值”:2,
“代码类型”:145,
“说明”:“同化”
},
{
“代码值”:3,
“代码类型”:145,
“说明”:“破产”
},
{
“代码值”:4,
“代码类型”:145,
“说明”:“投标书”
},
event.interface.ts
导出接口IEventerface{
代码值:数字;
代码类型:数字;
描述:字符串;
}
服务台
getEventType():
{
返回此.http.get('/gpscaservices/v1/system-
描述?codeType=145&codeValue=');
}
创建.Component.ts
orderedEvent:任何;
事件类型:任意;
这是.\u dataService.getEventType().subscribe({
下一步:(响应:IEventerface[])=>{
this.eventTypes=响应;
这个。sortBy(‘描述’);
}
排序方式(字段:字符串){
this.eventTypes.sort((a:any,b:any)=>{
if(a[字段]b[field]){
返回1;
}否则{
返回0;
}
});
this.orderedEvent=this.eventTypes;
Create.Component.html
nx option*ngFor=“让orderedEvent.data的事件类型”
[值]=“eventType.codeValue”>
{{eventType.description}}
/nx选项>

您可以这样设置排序功能

sortEventTypes(sortString : string){
   this.eventTypes.sort(function (a, b) {
     return a[sortString] - b[sortString];
  });
}

我想你错过了
数据
字段

sortBy(字段:字符串){
this.eventTypes.data.sort((a:any,b:any)=>{
if(a[字段]b[field]){
返回1;
}否则{
返回0;
}
});
this.orderedEvent=this.eventTypes;
}
最好定义一个clear类型,而不是使用任何类型

导出接口事件数据{
数据:IEventerface[]
}
orderedEvent:EventData;
事件类型:EventData;

您想按升序还是降序排序?谢谢。效果很好。我花了几天时间才让它正常工作。我感谢您的帮助