我有一個數組:hobbies=['Reading','Sport','Travelling','Movies','Cooking','Singing'];
angular2填充ngFor在不同的網格單元
和模板現在我手動填充它:
<div class="my-checkbox">
<!-- Hobbies -->
<span style="float: left; width: 100px;"><h5>Hobbies</h5></span>
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--4-col">
<label #checkbox_1 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
<input type="checkbox" id="checkbox-1" name="hobbies" value="sport"
class="mdl-checkbox__input" >
<span class="mdl-checkbox__label">Sports</span>
</label>
<label #checkbox_2 class="mdl-checkbox mdl-js-checkbox" for="checkbox-2">
<input type="checkbox" id="checkbox-2" name="hobbies" value="reading" class="mdl-checkbox__input">
<span class="mdl-checkbox__label">Reading</span>
</label>
</div>
<div class="mdl-cell mdl-cell--4-col">
<label #checkbox_3 class="mdl-checkbox mdl-js-checkbox" for="checkbox-3">
<input type="checkbox" id="checkbox-3" name="hobbies" value="singing"
class="mdl-checkbox__input">
<span class="mdl-checkbox__label">Singing</span>
</label>
<label #checkbox_4 class="mdl-checkbox mdl-js-checkbox" for="checkbox-4">
<input type="checkbox" id="checkbox-4" name="hobbies" value="travelling"
class="mdl-checkbox__input">
<span class="mdl-checkbox__label">Travelling</span>
</label>
</div>
<div class="mdl-cell mdl-cell--4-col">
<label #checkbox_5 class="mdl-checkbox mdl-js-checkbox" for="checkbox-5">
<input type="checkbox" id="checkbox-5" name="hobbies" value="movies"
class="mdl-checkbox__input">
<span class="mdl-checkbox__label">Movies</span>
</label>
<label #checkbox_6 class="mdl-checkbox mdl-js-checkbox" for="checkbox-6">
<input type="checkbox" id="checkbox-6" name="hobbies" value="cooking"
class="mdl-checkbox__input">
<span class="mdl-checkbox__label">Cooking</span>
</label>
</div>
</div>
它看起來像以下:
我想改爲使用ngFor填充這些複選框,但保持結構完整,即替代值是inserte d爲連續的細胞是這樣的:
<div class="mdl-grid" *ngFor="let hobby of hobbies">
<div class="mdl-cell mdl-cell--4-col">
<label #checkbox_1 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
<input type="checkbox" id="checkbox-1" name="hobbies" value="sport"
class="mdl-checkbox__input" >
<span class="mdl-checkbox__label">{{hobby}}</span>
</label>
<label #checkbox_2 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
<input type="checkbox" id="checkbox-1" name="hobbies" value="sport"
class="mdl-checkbox__input" >
<span class="mdl-checkbox__label">{{hobby}}</span>
</label>
</div>
</div>
而且,因爲這是MDL-步進#checkbox_1
還是因爲我升級我的組件元素,比如下面的標籤也很重要:
@ViewChild('checkbox_2') checkbox_2:ElementRef;
@ViewChild('checkbox_3') checkbox_3:ElementRef;
@ViewChild('checkbox_4') checkbox_4:ElementRef;
@ViewChild('checkbox_5') checkbox_5:ElementRef;
@ViewChild('checkbox_6') checkbox_6:ElementRef;
componentHandler.upgradeElement(this.checkbox_1.nativeElement);
componentHandler.upgradeElement(this.checkbox_2.nativeElement);
componentHandler.upgradeElement(this.checkbox_3.nativeElement);
componentHandler.upgradeElement(this.checkbox_4.nativeElement);
componentHandler.upgradeElement(this.checkbox_5.nativeElement);
componentHandler.upgradeElement(this.checkbox_6.nativeElement);
我如何能實現這個?
UPDATE:
當我試圖與上述ngFor環路我得到這樣的以下內容:
看看使用各種組件的模式,看看你可以解壓縮到一個循環。你嘗試了什麼沒有奏效? – snorkpete
我基本上嘗試了與ngFor問題相同的代碼,但是它填充了相同的值而不是連續的值。另外,在動態填充時,我不確定如何給像#checkbox_1這樣的元素,以便可以升級該元素以進行適當的渲染 – Nitish
你在teamviewer中可用嗎? – Aravind