2017-07-04 25 views
0

をangular2している私は私が親コンポーネントで使用子コンポーネントfile-upload UIのすべてで複数の子コンポーネントが同じインスタンスが

<form> 
<fieldset> 
    <legend>Input Files</legend> 

    <file-upload id="s" imgpath="Image/saham.png" title="saham"></file-upload> 
    <file-upload id="q" imgpath="Image/sandoq.png" title="sandoq"></file-upload> 
    <file-upload id="o" imgpath="Image/oraq.png" title="oraq"></file-upload> 

    <button type="submit" class="btn btn-success" (click)="save()" [disabled]="!cansave">Save</button> 
</fieldset> 

以下のような複数回の罰金ですが、アクションでを持っていますfile-uploadというオブジェクトインスタンスが1つしか存在せず、file-uploadコンポーネントの各入力変更は、そのうちの1つ(最初のもの)にのみ適用されます。

問題はinputと私はそれを使用している方法です。私は単純なボタンを使用すると、すべての問題はありません。ここでは、それらはすべて、そのサービスの同じインスタンスを持っている理由だシングルトン FileServiceを持ってfile-upload

<div class="upload" (dragover)="allowDrop($event)" (drop)="drop($event)"> 
    <p>{{title}}</p> 
    <div class="drop-zone" [ngClass]="{'showdropzone' : showdropzone}"> 
     Drop Here Or... 
     <div class="clickhere"> 
      <label for="files">Click Here</label> 
      <input id="files" type="file" class="file" (change)="fileSelect($event)"><!--does not work--> 
      <button (click)="fileSelect($event)">Click Me</button> <!--this is working--> 
     </div> 
    </div> 
    <circle-progress class="myprogress" #circleProg1 [percent]="50" [ngClass]="{'showprogress' : showprogress}"></circle-progress> 
    <span class="glyphicon glyphicon-warning-sign status" [ngClass]="{'warninput' : haswarning}"></span> 
    <span class="glyphicon glyphicon-ok-circle status"  [ngClass]="{'successinput' : succeeded}"></span> 
</div> 
+0

Plunkrで例を共有する必要があります。何が間違っているか把握するのに十分なコードがありません。 –

+0

'file-upload'コンポーネントはサービスなどを共有していますか? – rinukkusu

+0

.tsファイルのコードを共有できますか? –

答えて

1

のhtmlです。私はあなたのAppModuleでプロバイダを注入するので、それを削除し、このfrom official docsを見て、複数のサービスインスタンスの詳細については、この

@Component){ 
    providers: [FileService] 
    ... 
} 
export class FileUploadComponent 

などのコンポーネントレベルであなたのサービスを注入しようとすると仮定しています。 ほしいと思っています。

+0

'FileService'の' url'フィールドのみが使用されています - あなたは本当にそれが問題だと思いますか? – rinukkusu

+0

@rinukku FileServiceのurlフィールドだけを使用するかどうかは関係ありません。シングルトンの場合、子コンポーネントはこのインスタンスを共有します。私の答えを試しましたか? :) – brijmcq

+0

私は答えを試みました。しかし、これのためではありません。私は問題を発見したが、私はそれを解決する方法を知らない。それはファイルアップローダのhtmlにあり、それは 'input'のためです。私は正確な問題を表示するために私の質問を更新しました@brijmcq – Parid0kht

関連する問題