2017-02-21 7 views
1

私は3または4のような数値を指定する入力ボックスを持っています。その下に親divがあります。入力ボックスに指定された番号に従って、私は親の中にその多くのdivを持っていたい。div2を動的に追加するAngular2

 <div class="mdl-step__content"> 
      <!-- specify number --> 
      <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> 
       <input class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?" id="text5"> 
       <label class="mdl-textfield__label" for="text5">Number of important collegues</label> 
       <span class="mdl-textfield__error">Number required!</span> 
      </div> 
      <!-- circular divs --> 
      <div class="parent"> 
       <div class="circle"></div> 
       <div class="circle"></div> 
       <div class="circle"></div> 
       <div class="circle"></div> 
      </div> 
     </div> 

どうすれば実現できますか?

UPDATE: enter image description here

答えて

3

あなたは、この目的のためにngForを使用することができます。

<input ngModel (ngModelChange)="updateNumber($event)" class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?" id="text5"> 

<div class="parent"> 
    <div class="circle" *ngFor="let item of items"></div> 
updateNumber(val:number) { 
    this.items = new Array(+val); 
} 
+0

それはする必要があります: '新しい配列(val)で'?また、関数からセットを削除しなければなりませんでした。しかし、それは '* ngFor ="そのdivのアイテム "'にアイテムを追加させません。それは言う:プロパティ結合ngForIn埋め込まれたテンプレート上の任意のディレクティブで使用されていません。 – Nitish

+0

うわー、ごめんなさい!私のより卑劣な答えの一つに見えます。 'in'ではなく' of'でなければなりません。 –

+0

これでエラーは起こりませんが、何番に入っても1つのdivになります。私の質問にイメージを追加する! – Nitish

関連する問題