Angular 数组拼接和推送不工作

Angular Array Splice and Push not working

我有一个奇怪的问题,我使用的是数据视图,所以当我将项目添加到数组时并没有推送到数组。我使用 chrome 开发人员工具来确认没有任何内容被推送到数组。

HTM(为简单起见仅显示复选框)

      <p-dataView [value]="iErsaPreviewApps" [paginator]="true" [rows]="20" [sortField]="sortField" [sortOrder]="sortOrder" paginatorPosition="both">
         <ng-template let-prev pTemplate="listItem">
             <input type="checkbox" id="cbPreviewID" checked name="cbxPreview" (click)="togglePreviewApp($event)" style="margin-right:5px;margin-bottom:10px;margin-left:5px; margin-top:10px" [value]='prev.app_id'> {{prev.app_name}}
       </ng-template>
     </p-dataView>

界面

export interface IErsaApps {
        app_id: number;
        app_type_id: number;
        app_name: string;
        app_roles: string;
        app_sort_id?: number;
       roles: Array<IErsaAppRoles>
}
export interface IErsaAppRoles {
    app_role_id: number;
    app_role_app_id: number;
    app_role_name: string;
    app_role_sort_id?: number;
}

服务

getErsaApps(): Observable<IErsaApps[]> {
        return this._http.get(environment.BASE_API_URL + 'xxxxxx')
            .map((response: Response) => <IErsaApps[]>response.json())
            .catch(this.handleError);
    }

TS

iErsaAppList: IErsaApps[] = []; 
    selectedObject: IErsaApps; 
    selectedPreviewObject: IErsaApps; 
    iErsaDeafultApps: IErsaApps[] =[]; 
    iErsaPreviewApps: IErsaApps[]=[]; 
    iErsaSelectedApps: IErsaApps[] = null; 
 ngOnInit() {
 this.GetErsaApps();
}
 GetErsaApps() {
        this._ersaDet.getErsaApps()
            .subscribe(
            data => {
            this.iErsaDefaultApps = data;
            this.iErsaAppList = data.map(item => item);      },
       }
       togglePreviewApp(event: any) {
        if (this.iErsaAppList != undefined) {
              this.selectedPreviewObject = this.iErsaAppList
                .find(x => x.app_id == event.srcElement.value);
            const index: number = this.iErsaPreviewApps.indexOf(this.selectedPreviewObject);
            this.iErsaDeafultApps.push(this.selectedPreviewObject);
        }
    }  

很确定这一行是你的问题,你在其中对数组执行 Object.assign :

this.iErsaAppList = Object.assign([], this.iErsaDeafultApps); },

我在我的控制台中这样试过:

const obj = {a:'a'};
const result = Object.assign([],obj);
// result -> [a: "a"]
// Try to access properties of array.
const item = result[0]; // undefined

我不太确定那个结果数组发生了什么,但它看起来不像我以前看到的任何东西。我不知道您从该提取中得到的响应是什么样的,但假设它是一个数组,您在接收数据时可能需要这样的代码。

 this._ersaDet.getErsaApps()
        .subscribe(data => {
           this.iErsaApp.list = data.map(item=> item);  // map response to new array
});