如何在Angular 8中使用来自HTML输入的JSON文件?

huangapple go评论119阅读模式
英文:

How to use JSON file from HTML input in Angular 8?

问题

在我的当前Angular 8项目中,我有两个JSON文件,它们应该连接成一个JSON数组。然后应该导出新文件。

在经过长时间的Google搜索后,我没有找到解决办法,无法使用两个HTML输入调用的两个JSON文件,以便我可以在Angular组件中将这两个文件的输入组合成一个JSON数组。

我的当前代码:

JsonConverterComponent.html

  1. <div class="form-group">
  2. <label for="Json1">Json 1</label>
  3. <input [(ngModel)]="json1" type="file" id="Json1" required>
  4. </div>
  5. <div class="form-group">
  6. <label for="Json2">Json 2</label>
  7. <input [(ngModel)]="json2" type="file" id="Json2" required>
  8. </div>
  9. <button [buttonLabel]="buttonNameJson" [disableCheck]="!json1 || !json2" (click)="combineJSON()"></button>

JsonConverterComponent.TS

  1. export class JsonConverterComponent implements OnInit {
  2. constructor(private http: HttpClient) { }
  3. ngOnInit() {
  4. }
  5. buttonNameJson = "Combine JSON";
  6. json1: File;
  7. json2: File;
  8. test: File;
  9. one = this.json1;
  10. two = this.json2;
  11. public combineJSON() {
  12. this.test = this.one;
  13. console.log(this.test);
  14. }
  15. }

如果我只想调用两个导入的JSON文件中的一个的内容,我总是得到"undefined"错误。

我应该怎么做才能在JsonConverterComponent.ts中使用单独的JSON?

英文:

In my current Angular 8 project I have two JSON files which should be connected to one JSON array. The new file should then be exported.

After googling for a long time I didn't find a solution how to use the two JSON files, which I call up via two HTML inputs, so that I can combine both files in the Angular Component to form a JSON array with input from both JSON files.

My current code:

JsonConverterComponent.html

  1. &lt;div class=&quot;form-group&quot;&gt;
  2. &lt;label for=&quot;Json1&quot;&gt;Json 1&lt;/label&gt;
  3. &lt;input [(ngModel)]=&quot;json1&quot; type=&quot;file&quot; id=&quot;Json1&quot; required&gt;
  4. &lt;/div&gt;
  5. &lt;div class=&quot;form-group&quot;&gt;
  6. &lt;label for=&quot;Json2&quot;&gt;Json 2&lt;/label&gt;
  7. &lt;input [(ngModel)]=&quot;json2&quot; type=&quot;file&quot; id=&quot;Json2&quot; required&gt;
  8. &lt;/div&gt;
  9. &lt;button [buttonLabel]=&quot;buttonNameJson&quot; [disableCheck]=&quot;!json1 || !json2&quot; (click)=&quot;combineJSON()&quot;&gt;&lt;/button&gt;

JsonConverterComponent.TS

  1. export class JsonConverterComponent implements OnInit {
  2. constructor(private http: HttpClient) { }
  3. ngOnInit() {
  4. }
  5. buttonNameJson = &quot;Combine JSON&quot;;
  6. json1: File;
  7. json2: File;
  8. test: File;
  9. one = this.json1;
  10. two = this.json2;
  11. public combineJSON() {
  12. this.test = this.one;
  13. console.log(this.test);
  14. }
  15. }

If I just want to call up the content of one of the two imported JSON files, I always get the error "undefined".

What do I have to do so that I can use the individual JSON in JsonConverterComponent.ts?

答案1

得分: 3

ngModel指令不能用于input类型为file的元素,您需要通过以下方式对change事件做出响应:

  1. <input (change)="onFileChange($event)" type="file" id="Json1" required>
  2. <input (change)="onFileChange($event)" type="file" id="Json2" required>

然后在TypeScript文件中:

  1. onFileChange(event){
  2. const fileToLoad = event.target.files[0];
  3. const fileReader = new FileReader();
  4. fileReader.onload = function(fileLoadedEvent){
  5. const textFromFileLoaded = fileLoadedEvent.target.result;
  6. const json = JSON.parse(textFromFileLoaded);
  7. console.log(json);
  8. };
  9. fileReader.readAsText(fileToLoad, "UTF-8");
  10. }
英文:

The ngModel directive can't be used on a input of type file, you have to react on the change event like this:

  1. &lt;input (change)=&quot;onFileChange($event)&quot; type=&quot;file&quot; id=&quot;Json1&quot; required&gt;
  2. &lt;input (change)=&quot;onFileChange($event)&quot; type=&quot;file&quot; id=&quot;Json2&quot; required&gt;

Then in the TypeScript file:

  1. onFileChange(event){
  2. const fileToLoad = event.target.files[0];
  3. const fileReader = new FileReader();
  4. fileReader.onload = function(fileLoadedEvent){
  5. const textFromFileLoaded = fileLoadedEvent.target.result;
  6. const json = JSON.parse(textFromFileLoaded);
  7. console.log(json);
  8. };
  9. fileReader.readAsText(fileToLoad, &quot;UTF-8&quot;);
  10. }

huangapple
  • 本文由 发表于 2020年1月6日 20:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定