Created
January 26, 2019 15:08
-
-
Save alegut/6de30437bf9d1c3621f440044ce59014 to your computer and use it in GitHub Desktop.
Display an image using Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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)"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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