我有一个奇怪的问题,我使用的是数据视图,因此当我向数组中添加项目时不会推送到数组。我使用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);
}
}
答案 0 :(得分:2)
可以肯定的是,这是您遇到的问题,您可以在其中将Object.assign分配给数组:
let counter (text : string) (pattern : string) =
let rec countFrom (i:int) total =
match text.IndexOf(pattern, i) with
| j when j >= 0 -> countFrom (j+pattern.Length) (total+1)
| _ -> total
countFrom 0 0
我在控制台中这样尝试过:
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