angular 按钮加载本地 json 文件以填充数据对象
angular button load local json file to fill data object
小伙伴们,我想做的是用一个按钮加载本地文件,然后把文件数据转成jsonObject。
.html:
<div class="uuidLableLeft"> <b>Do your want to import your previous file (.json)?</b>
<input style="display: none" type="file" accept=".json"
(change)="onFileLoad($event)" #fileinput>
<button type="button" (click)="fileinput.click()"> load </button>
</div>
...
...
<div>
<textarea class="form-control" placeholder="" [(ngModel)]="cUser.vision" ></textarea>
</div>
.ts:
onFileLoad (event) {
const f = event.target.files[0];
const reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
try {
const json = JSON.parse(e.target.result);
const resSTR = JSON.stringify(json);
this.cUser = JSON.parse(resSTR);
console.log('... uuid of cUser: ', this.cUser.id);
} catch (ex) {
alert('exception when trying to parse json = ' + ex);
}
};
})(f);
reader.readAsText(f);
}
问题是:
- this.cUser 不会将更改传递给 html
- 修改 "undefined"
的消息
如果我通过提供静态路径加载文件,它就可以工作,
this.http.request('assets/Your_filename.json')
但是我怎样才能通过导入按钮来完成呢?
或者有其他不使用文件 reader 的方法吗?非常感谢!!
问题是上下文 (this
) 对象。 this
对象应该指向 FileReader
在你的情况下。您可以使用箭头功能来解决这个问题:
cUser: any;
onFileLoad(event) {
const f = event.target.files[0];
const reader = new FileReader();
reader.onload = ((theFile) => {
return (e) => {
try {
const json = JSON.parse(e.target.result);
const resSTR = JSON.stringify(json);
this.cUser = JSON.parse(resSTR);
console.log('... uuid of cUser: ', this.cUser.id);
} catch (ex) {
alert('exception when trying to parse json = ' + ex);
}
};
})(f);
reader.readAsText(f);
}
小伙伴们,我想做的是用一个按钮加载本地文件,然后把文件数据转成jsonObject。
.html:
<div class="uuidLableLeft"> <b>Do your want to import your previous file (.json)?</b>
<input style="display: none" type="file" accept=".json"
(change)="onFileLoad($event)" #fileinput>
<button type="button" (click)="fileinput.click()"> load </button>
</div>
...
...
<div>
<textarea class="form-control" placeholder="" [(ngModel)]="cUser.vision" ></textarea>
</div>
.ts:
onFileLoad (event) {
const f = event.target.files[0];
const reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
try {
const json = JSON.parse(e.target.result);
const resSTR = JSON.stringify(json);
this.cUser = JSON.parse(resSTR);
console.log('... uuid of cUser: ', this.cUser.id);
} catch (ex) {
alert('exception when trying to parse json = ' + ex);
}
};
})(f);
reader.readAsText(f);
}
问题是:
- this.cUser 不会将更改传递给 html
- 修改 "undefined" 的消息
如果我通过提供静态路径加载文件,它就可以工作,
this.http.request('assets/Your_filename.json')
但是我怎样才能通过导入按钮来完成呢?
或者有其他不使用文件 reader 的方法吗?非常感谢!!
问题是上下文 (this
) 对象。 this
对象应该指向 FileReader
在你的情况下。您可以使用箭头功能来解决这个问题:
cUser: any;
onFileLoad(event) {
const f = event.target.files[0];
const reader = new FileReader();
reader.onload = ((theFile) => {
return (e) => {
try {
const json = JSON.parse(e.target.result);
const resSTR = JSON.stringify(json);
this.cUser = JSON.parse(resSTR);
console.log('... uuid of cUser: ', this.cUser.id);
} catch (ex) {
alert('exception when trying to parse json = ' + ex);
}
};
})(f);
reader.readAsText(f);
}