javascript – ngFor在Angular 2中从promise中填充的对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – ngFor在Angular 2中从promise中填充的对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2230字,纯文字阅读大概需要4分钟。
内容图文
![javascript – ngFor在Angular 2中从promise中填充的对象](/upload/InfoBanner/zyjiaocheng/767/08fa98259ca14604a2fd4ba01c1b9597.jpg)
我正在使用filesFromZip对象中的blobURL解析带有图像的zip文件,并使用ngFor迭代它以在页面上显示图像.
看起来像这样
filesFromZip = {};
let zip = new JSZip();
zip.loadAsync(zipfileFromInput)
.then(function (zip) {
for (let file in zip.files) {
let fileInZip = zip.files[file];
zip.file(fileInZip.name)
.async("arraybuffer")
.then(function (content) {
let buffer = new Uint8Array(content);
let blob = new Blob([buffer.buffer]);
// here is where I want to push this object into the filesFromZip object somehow....
return {
fileName: fileInZip.name,
blobURL: URL.createObjectURL(blob)
};
});
}
});
在模板中:
<md-card *ngFor="let file of filesFromZip">
<h3 md-line>{{file.fileName}}</h3>
<img src="{{file.blobURL}}">
</md-card>
如何从promiseFromZip获取数据?
我试图将它推入filesFromZip,但这给了我这个错误:
error_handler.js:47 EXCEPTION: Uncaught (in promise): TypeError:
Cannot read property ‘async’ of null
我真的在与异步和Angular 2争吵.有人可以把我送到正确的方向吗?
解决方法:
首先,您不能使用* ngFor指令迭代普通对象,因此filesFromZip应该是一个数组.
this.filesFromZip = [];
zip.loadAsync(zipfileFromInput)
.then((zip)=> {
for (let file in zip.files) {
let fileInZip = zip.files[file];
zip.file(fileInZip.name)
.async("arraybuffer")
.then((content)=> {
let buffer = new Uint8Array(content);
let blob = new Blob([buffer.buffer]);
// here is where I want to push this object into the filesFromZip object somehow....
this.filesFromZip.push({
fileName: fileInZip.name,
blobURL: URL.createObjectURL(blob)
});
});
}
});
或者你可以使用Promise.all(),它将从可迭代的Promise中创建一个promise,然后使用异步管道:
javascript:
this.filesFromZip = Promise.all(
zip.loadAsync(zipfileFromInput)
.then((zip)=> {
let out=[];
for (let file in zip.files) {
let fileInZip = zip.files[file];
out.push(zip.file(fileInZip.name)
.async("arraybuffer")
.then((content)=> {
let buffer = new Uint8Array(content);
let blob = new Blob([buffer.buffer]);
// here is where I want to push this object into the filesFromZip object somehow....
this.filesFromZip.push({
fileName: fileInZip.name,
blobURL: URL.createObjectURL(blob)
});
}));
}
return out;
});
)
模板:
<md-card *ngFor="let file of filesFromZip|async">
<h3 md-line>{{file.fileName}}</h3>
<img src="{{file.blobURL}}">
</md-card>
但是,这里的主要问题似乎是zip.file(fileInZip.name)返回null
内容总结
以上是互联网集市为您收集整理的javascript – ngFor在Angular 2中从promise中填充的对象全部内容,希望文章能够帮你解决javascript – ngFor在Angular 2中从promise中填充的对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。