2017-08-23 2 views
1

私はユーザーがローカルイメージを選択できるページを作成しようとしています。それが表示されます。私はウェブ世界には初めてのので、私はたくさんのGoogle検索を行い、基本的にいくつかの他のStackOverflow記事をコピーしました。それは仕事が必要です。しかし、それはしません。ファイルがロードされているのを見ることができます。イメージデータが要素に書き込まれていますが、要素は変更されません。代わりのテキストが表示されているだけで、白いままです。ブラウザにローカルイメージファイルを表示する

app.component.html:

<div style="text-align:center"> 
    <h1>Sprite Sheet Converter</h1> 
</div> 

<div class="container"> 
    <form> 
    <div class="row"> 
     <label class="control-label" for="sourceFileControl">Source:</label> 
     <input class="form-control" type="file" class="form-control" id="sourceFileControl" name="sourceFileControl" (change)="load($event)" /> 
    </div> 

    <div class="row"> 
     <img #sourceImage src="" alt="Source" width="100" height="100"/> 
    </div> 

    </form> 

    <img #destinationImage alt="Destination"/> 
</div> 

app.component.ts:

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

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    @ViewChild('sourceImage') sourceImage: HTMLImageElement; 
    @ViewChild('destinationImage') destinationImage: HTMLImageElement; 

    load(event: EventTarget): void { 
    const eventObj: MSInputMethodContext = <MSInputMethodContext> event; 
    const target: HTMLInputElement = <HTMLInputElement> eventObj.target; 
    const files: FileList = target.files; 
    const file = files[0]; 
    console.log(file); 

    const reader = new FileReader(); 
    const img = this.sourceImage; 

    reader.onloadend = function() { 
     img.src = reader.result; 
     console.log('done loading'); 
     console.log(img); 
    }; 

    console.log('now loading'); 
    reader.readAsDataURL(file); 
    } 
} 

結果に(

デバッグ出力をファイルを選択した後すべてのメソッドが呼び出され、画像だけが空ですそしてそれは、代替テキストです示す。)

enter image description here

だから... ...私が間違って何をやっているの?

+1

、選択した画像は、* *単語「ソースではありません"白い背景に... – nvoigt

答えて

1

img要素に幅と高さのスタイルを与えるようにしてください。代わりにarrow function() =>を使用する必要があります。また、特定のケースではViewChildは必要ありません。

を使用して、データがあるまで画像が読み込まれないようにしてください。ソースイメージデータを含む変数にイメージソースをバインドします。宛先イメージにも同じことが起こります。

<img *ngIf="sourceImage" src={{sourceImage}} alt="Source" width="100" height="100"/> 

<img *ngIf="destinationImage" src={{destinationImage}} alt="Destination"/> 

...とあなたcomponent.tsのコードは次のようになります。次へ<img>のタグを変更するだけでレコードの

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

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    sourceImage: string; 
    destinationImage: string; 

    load(event: EventTarget): void { 
    const eventObj: MSInputMethodContext = <MSInputMethodContext> event; 
    const target: HTMLInputElement = <HTMLInputElement> eventObj.target; 
    const files: FileList = target.files; 
    const file = files[0]; 
    console.log(file);  
    const reader = new FileReader();  
    reader.onloadend =() => { 
     console.log('done loading'); 
     this.sourceImage = reader.result; 

    }; 

    console.log('now loading'); 
    reader.readAsDataURL(file); 
    } 
} 
+0

ああ、それは本当に簡単です。ありがとう、素晴らしい作品! – nvoigt

+0

うれしかった:) – Faisal

-2

DOMのSRC属性を取得しているかどうかを確認してください。それがあれば、適切な画像ソースが存在するかどうかをチェックする。あなたがfunctionキーワードを使用する場合、あなたはthisへのアクセスを失う

+0

私の画像要素は*幅と高さを持っています。私のDOMでは、 'src'は実際に'未知 'と言います。私はこれを解決するために何をするかもしれないか? – nvoigt

+1

質問を理解できない場合は、回答を投稿しないでください。ここでの質問は** not ** _です。静的なHTMLの部分には、リソースを指すsrc属性を持つイメージタグがありますが、 "_、もしあれば_"を表示しません。 src属性 "_はとにかく悪い答えです。 – CodeCaster

関連する問題