arrays - 访问对象数组中的特定对象属性时返回对象承诺?(角度 - 离子)
问题描述
我有一个对象数组,我希望能够在该数组中的单个对象中提及特定属性。到目前为止,我所做的如下:
我将我的对象数组存储在 Ionics 存储中。然后我使用异步函数将对象数组分配给我的打字稿文件中的数组:这可以在这里看到;
//The new array
users: User[] = [];
//The async function
async readUsers() {
this.users = await this.service.readUsers();
this.index = this.users.findIndex((x => x.id == this.passed_id));
return this.users[this.index];
}
async 函数将数组分配给 this.users,然后我找到我想要的对象并将其索引分配给 this.index,然后我用它来识别 this.users 数组中的所需对象。直到这里的一切都很好,我设法在存储的数组中获取所需的对象。
然后我可以使用以下方法读取所选对象的属性:
“alert(this.users[this.index].firstName)”,而不是异步函数中的最后一行。这也很好。
但是,现在我想在表单构建器控件中提及特定对象,如下所示:
firstName: [this.readUsers(), Validators.required],
这不起作用,返回的只是 [Object Promise]。为了从上述对象数组的表单输入中读取特定属性(作为文本),我可以在这里做什么?任何帮助表示赞赏。
解决方案
您必须等待this.readUsers()
展开的值。
async method() {
...
firstName: [await this.readUsers(), Validators.required],
...
}
也许对你来说,你必须做这样的事情:
form: any; // using any, but you can change the type to what FormBuilder returns
....
async ngOnInit() {
const users = await this.readusers();
this.form = this.formBuilder.group({
firstName: [users, Validators.required],
});
}
推荐阅读
- flutter - 无法使用底部导航栏构建可滚动页面
- gephi - 将节点大小按比例更改为属性数据
- c++ - IMFSinkWriter::BeginWriting 未初始化,因为 IMFMediaEventGenerator 有未决事件
- excel - 如何根据单元格的左值在两个值之间计算单元格
- angular - Angular (8) Universal with firebase cloud 功能 - 无法匹配任何路由
- html - 修复了导航和锚点偏移问题
- python - 如何在 Keras 中打印两个张量之间的余弦相似度值?
- java - Maven 不会将 jar 复制到类路径(使用 STS)
- json - 如何修复“AssertionError:keyType 应该是 DataType”错误
- javascript - Math.js 错误:无法将“abc”转换为数字