我有一个数组:hobbies=['Reading','Sport','Travelling','Movies','Cooking','Singing'];
现在在模板中我手动填充它:
<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来填充这些复选框,但保持结构完整,即将替换值插入到连续的单元格中,如:
<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-stepper #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);
我怎样才能做到这一点?
更新
当我尝试使用上面提到的ngFor循环时,我得到以下内容:
答案 0 :(得分:4)
您可以创建将数组拆分为2个元素块的管道。它可能看起来像这样:
<强> chunks.pipe.ts 强>
@Pipe({ name: 'chunks' })
export class ChunksPipe implements PipeTransform {
transform(arr: any, chunkSize: number) {
return arr.reduce((prev, cur, index) => (index % chunkSize) ? prev : prev.concat([arr.slice(index, index + chunkSize)]), []);
}
}
然后你可以像这样使用ChunksPipe
:
<强> component.html 强>
<div class="mdl-grid">
<div *ngFor="let chunk of hobbies | chunks: 2" class="mdl-cell mdl-cell--4-col">
<label *ngFor="let hobby of chunk" #checkbox class="mdl-checkbox mdl-js-checkbox">
<input type="checkbox" name="hobbies" [value]="hobby" class="mdl-checkbox__input">
<span class="mdl-checkbox__label">{{hobby}}</span>
</label>
</div>
</div>
要升级元素,我会通过@ViewChildren
<强> component.ts 强>
hobbies = ['Sport', 'Reading', 'Singing', 'Travelling', 'Movies', 'Cooking' ];
@ViewChildren('checkbox') checkboxes: QueryList<ElementRef>;
ngAfterViewInit() {
this.checkboxes.forEach(cbx => componentHandler.upgradeElement(cbx.nativeElement));
}
P.S。替代方法是使用将在内部执行的指令
<强> Plunker Example 强>
另请参阅 Grid Example