Skip to content

Instantly share code, notes, and snippets.

@alegut
Created January 26, 2019 15:08
Show Gist options
  • Save alegut/6de30437bf9d1c3621f440044ce59014 to your computer and use it in GitHub Desktop.
Save alegut/6de30437bf9d1c3621f440044ce59014 to your computer and use it in GitHub Desktop.
Display an image using Typescript
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<img [src]="url" height="200"> <br/>
<input type='file' (change)="onSelectFile($event)">
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 4';
url = '';
onSelectFile(event) {
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(event.target.files[0]); // read file as data url
reader.onload = (event) => { // called once readAsDataURL is completed
this.url = event.target.result;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment