0

I am trying to do something very basic in angular . The idea is to just read a log file and display the same in tabular format after processing it and filtering it.

I have completed the heroes tutorial. My project has just one component.

app.component.html

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>

<app-maindisplay></app-maindisplay>

maindisplay.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-maindisplay',
  templateUrl: './maindisplay.component.html',
  styleUrls: ['./maindisplay.component.css']
})
export class MaindisplayComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  handleFile(InputFileList: FileList): void {
    console.log('The Handle file got called');
  }
}

maindisplay.component.html

<div style="text-align:left">
  <input type="file" id="LogFileInput" onchange="handleFile(this.files)">
</div>

File dialogue comes up correctly The file dialogue box comes up correctly.

However, when the file is selected on chrome console, following error message comes up.

Uncaught ReferenceError: handleFile is not defined at HTMLInputElement.onchange (localhost/:13)

Can you please help me fix this issue ? Once the function is called I would be able to handle the file manipulation.

Best Regards, Vinayak

1
  • Happy to help you Commented Jan 19, 2018 at 17:39

1 Answer 1

1

You should be using (change) and pass the $event to the method

<input type="file" id="LogFileInput" (change)="handleFile($event)">

You can access the file in your method using

event.target.files

Typescript method should look like

handleFile(event): void {
   this.files = event.target.files;
   console.log('The Handle file got called');
   console.log(this.files); // logs selected files
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.