首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular2在不同网格单元中填充ngFor

angular2在不同网格单元中填充ngFor
EN

Stack Overflow用户
提问于 2017-04-02 10:30:51
回答 1查看 499关注 0票数 0

我有一个数组:hobbies=['Reading','Sport','Travelling','Movies','Cooking','Singing'];

现在,在模板中,我正在手动填充:

代码语言:javascript
复制
<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填充这些复选框,但是保持结构完整,即将交替值插入到连续的单元格中,如下所示:

代码语言:javascript
复制
<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或标签也很重要,因为我升级了组件中的元素,如下所示:

代码语言:javascript
复制
@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循环时,我发现如下所示:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-02 11:52:47

您可以创建将数组分成两个元素块的管道。看起来可能是这样的:

chunks.pipe.ts

代码语言:javascript
复制
@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

代码语言:javascript
复制
<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

代码语言:javascript
复制
hobbies = ['Sport', 'Reading', 'Singing', 'Travelling', 'Movies', 'Cooking' ];

@ViewChildren('checkbox') checkboxes: QueryList<ElementRef>;

ngAfterViewInit() {
  this.checkboxes.forEach(cbx => componentHandler.upgradeElement(cbx.nativeElement));
}

另一种方法是使用内部执行指令。

柱塞实例

另见网格实例

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43167285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档