In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to display all the heros is we hard cord all heros in our heroes component. First, in real world app, we cannot hard cord;  Seond, even we don't hard code, do http instead, it is still not good enough. We should leave Heroes component as dump component, just rendering the ui, no logic should be involved.

So instead of doing this in app.ts:

@Component({
selector: 'app',
template: `
<heroes>
</heroes>
`
})

We try another way like this:

@Component({
selector: 'app',
template: ` <heroes>
<hero name="Superman" id="1"></hero>
<hero name="Batman" id="2"></hero>
<hero name="BatGirl" id="3"></hero>
<hero name="Robin" id="4"></hero>
<hero name="Flash" id="5"></hero>
<hero name="Zhentian" id="6"></hero>
</heroes> `
})

Well, I know, still hard code, but just show how ngFor can be used.

Now, inside 'heroes' tag, we add now 'hero' tag. And we want to display those inside 'heroes' component:

import {Component, ContentChildren, QueryList} from "@angular/core";
import {Hero} from './hero';
/*
const HEROES = [
{id: 1, name:'Superman'},
{id: 2, name:'Batman'},
{id: 5, name:'BatGirl'},
{id: 3, name:'Robin'},
{id: 4, name:'Flash'}
];*/ @Component({
selector:'heroes',
styleUrls: [
'heroes.component.css'
],
template: `
<table>
<thead>
<th>Name</th>
<th>Index</th>
</thead>
<tbody>
<tr *ngFor="let hero of heroes; let i = index; trackBy: trackBy(hero);
let isEven=even; let isFirst=first; let isLast=last;"
[ngClass]="{'even': isEven, 'first': isFirst, 'last': isLast}">
<td>{{hero.name}}</td>
<td>{{i}}</td>
</tr>
</tbody>
</table>
`
})
export class Heroes {
//heroes = HEROES;
@ContentChildren(Hero)
heroes: QueryList<Hero> trackBy(hero){
return hero ? hero.id: undefined;
}
}

You can see, we have commented out the hard code array. Instead, we use:

    @ContentChildren(Hero)
heroes: QueryList<Hero>

'Hero' here, is a element directive:

import {Directive, Input} from "@angular/core";

@Directive({
selector: 'hero',
})
export class Hero { @Input()
id: number; @Input()
name:string; }

@ContentChildren will check the Children in HTML DOM tree, which will get:

                <hero name="Superman" id="1"></hero>
<hero name="Batman" id="2"></hero>
<hero name="BatGirl" id="3"></hero>
<hero name="Robin" id="4"></hero>
<hero name="Flash" id="5"></hero>
<hero name="Zhentian" id="6"></hero>

QueryList<Hero>: Only get 'Hero' directive.

QueryList is a class provided by Angular and when we use QueryList with a ContentChildren Angular populate this with the components that match the query and then keeps the items up to date if the state of the application changes .

However, QueryList requires a ContentChildren to populate it, so let’s take a look at that now.

What's cool about *ngFor, it not only accpets Array, but also any iterable type, we have list of DOM element 'hero', which are iterable, so ngFor will able to display those also.

[Angular 2] More on *ngFor, @ContentChildren & QueryList<>的更多相关文章

  1. [Angular] Create a simple *ngFor

    In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...

  2. angular 中数据循环 *ngFor

    <!--The content below is only a placeholder and can be replaced.--> <div style="text-a ...

  3. [Angular 2] ng-model and ng-for with Select and Option elements

    You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...

  4. [Angular] @ViewChildren and QueryLists (ngAfterViewInit)

    When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This ...

  5. Angular 星级评分组件

    一.需求演变及描述: 1. 有一个“客户对公司的总体评价”的字段(evalutation).字段为枚举类型,0-5,对应关系为:0-暂无评价,1-很差,2-差,3-一般,4-好,5-很好 2. 后来需 ...

  6. ng-bootstrap 组件集中 tabset 组件的实现分析

    ng-bootstrap: tabset 本文介绍了 ng-bootstrap 项目中,tabset 的实现分析. 使用方式 <ngb-tabset> 作为容器元素,其中的每个页签以一个 ...

  7. ng-template、ng-content、ng-container

    https://www.jianshu.com/p/0f5332f2bbf8 ng-template.ng-content.ng-container三者应该是自定义组件需要经常用到的指令.今天咱们就来 ...

  8. Angular6 学习笔记——内容投影, ViewChild和ContentChild

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  9. Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)

    原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...

随机推荐

  1. [转] arcgis Engine创建shp图层

    小生 原文 arcgis Engine创建shp图层 以创建点图层为例.首先要得到保存文件的地址. SaveFileDialog saveFileDialog = new SaveFileDialog ...

  2. UIImage 缩放等效果处理

    //等比率缩放 - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ // 创建一个bitmap的context // 并把它设置成为当 ...

  3. SQL对字符串进行排序

    假设字符串中只由'A'.'B'.'C'.'D'组成,且长度为7.并设函数REPLICATE(<字符串>,<n>)可以创建一个<字符串>的n个副本的字符串,另外还有R ...

  4. 如何避免JavaScript的内存泄露及内存管理技巧

    发表于谷歌WebPerf(伦敦WebPerf集团),​​2014年8月26日. 高效的JavaScript Web应用必须流畅,快速.与用户交互的任何应用程序,都需要考虑如何确保内存有效使用,因为如果 ...

  5. HDU5780 gcd 欧拉函数

    http://acm.hdu.edu.cn/showproblem.php?pid=5780 BC #85 1005 思路: 首先原式化简:x​^gcd(a,b)​​−1 也就是求n内,(公约数是i的 ...

  6. 给MyEclipse 10增加SVN功能

    1.在myeclipse的安装目录下 myeclipse 10文件夹下的 dropins文件夹新建一个文件夹 svn. 2.然后下载SVN插件:svn插件网站:http://subclipse.tig ...

  7. Google Appengine参考路径

    1.Hello, World! in 5 minutes 2.Creating a Guestbook -Introduction 3.Sample Applications 1.Programmin ...

  8. lipo命令

    工作中,xcode工程遇到一个bug file was built for archive which is not the architecture being linked armv7 找了一些资 ...

  9. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  10. java assert的用法简介

    assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...