![](/img/trans.png)
[英]How can I Iterate through an Object and Display it as a key value pair in Angular?
[英]How can I set a value to an key value pair property in Angular?
假设我有给定的 object:
export interface File {
id: string;
displayname: string;
filesize: number;
isarchived: boolean;
userId: string;
[key: string]: any;
}
和以下方法:
export class BasketService {
addFile(file: File ) { //Some code here }
}
比我这样称呼我的方法:
this._basketService.addFile({
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
???: file.originalEntry // <=== How can I set the key value pair here?
});
object:
file.originalEntry is [key: string]
。 我想传递整个 object 而不是单个键/值对。
试试这样:
this._basketService.addFile({
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
keyValue: {key:file.id, value: file.originalEntry}
});
如果声明是这样的:
public dictionary: { [key: string]: string } = {};
你可以这样设置它的值:
this.dictionary = { ID: key, Value: defaultValue }; // key and defaultValue are variables
在你的情况下通过这个:
{ ID: key, Value: defaultValue }
我解决了它,在我的 object 中明确放置我需要的键/值对,如下所示:
const digitalObject = {
id: file.id,
displayname: file.originalEntry['displayname'],
filesize: file.originalEntry['filesize'],
isarchived: file.originalEntry['isarchived'],
userId: this._appSession.user.id,
code: file.originalEntry['code'],
needle: file.originalEntry['needle'],
x8: file.originalEntry['x8'][0],
x5: file.originalEntry['x5'][0],
8: file.originalEntry['8'][0],
5: file.originalEntry['5'][0],
creationtime: file.creationTime
};
this._basketService.addFile(digitalObject);
感谢所有的贡献。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.