Roka545 Roka545 - 1 year ago 97
AngularJS Question

Calling a "function" inside a TypeScript file to HTML

I'm fairly new to HTML, TypeScript, and JavaScript. I'm currently trying to get the relative path from a selected folder. I'm using code from this Stack post. I've added the HTML for selecting a folder but I'm having trouble with the JavaScript portion. This is an Angular 2 app so I have a TypeScript file. I've added my code below as well as a JSFiddle link after it.

This is my HTML:

<div class="panel panel-primary" style="border-color: #464646;">
<div class="panel-heading" style="border-color: #BBBBBB; height: 35px; padding-top: 3px;">
<div style="float: left; margin-top: 5px;">Select directory</div>
<div class="panel-body">
<!--Directory Selection Button-->
<div class="row">
<input type="file" id="FileUpload" onchange="selectFolder(event)" webkitdirectory mozdirectory msdirectory odirectory directory multiple />

and this is my TypeScript file:

import {Component} from '@angular/core';
import {HttpModule} from "@angular/http";

selector: 'home',
templateUrl: './SDI/templates/home.html',
export class HomeComponent {

constructor(){ }

selectFolder(e: any) {
console.log('selectFolder entered.');

var theFiles =;
var relativePath = theFiles[0].webkitRelativePath;
var folder = relativePath.split("/");

JSFiddle link:

However, when I run the application and select a folder, my console tells me

selectFolder is not defined.

What exactly am I doing wrong?

Answer Source

You should use (change) instead of onchange attribute. Basically onchange event looks for function in global javascript scope(context), whereas when you wanted to call Component method associated with template, you have to follow (eventName)(event name in paranthesis) convention to call component function.

Additionally do use $event while passing parameter to function instead of event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download