[Angular 2] More on *ngFor, @ContentChildren & QueryList<>
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<>的更多相关文章
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- angular 中数据循环 *ngFor
<!--The content below is only a placeholder and can be replaced.--> <div style="text-a ...
- [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 ...
- [Angular] @ViewChildren and QueryLists (ngAfterViewInit)
When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This ...
- Angular 星级评分组件
一.需求演变及描述: 1. 有一个“客户对公司的总体评价”的字段(evalutation).字段为枚举类型,0-5,对应关系为:0-暂无评价,1-很差,2-差,3-一般,4-好,5-很好 2. 后来需 ...
- ng-bootstrap 组件集中 tabset 组件的实现分析
ng-bootstrap: tabset 本文介绍了 ng-bootstrap 项目中,tabset 的实现分析. 使用方式 <ngb-tabset> 作为容器元素,其中的每个页签以一个 ...
- ng-template、ng-content、ng-container
https://www.jianshu.com/p/0f5332f2bbf8 ng-template.ng-content.ng-container三者应该是自定义组件需要经常用到的指令.今天咱们就来 ...
- Angular6 学习笔记——内容投影, ViewChild和ContentChild
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
原文链接 Understanding ViewChildren, ContentChildren, and QueryList in Angular 使用场景 有时候,我们想要在父组件中访问它的子组件 ...
随机推荐
- Android Scrollview 内部组件android:layout_height="fill_parent"无效的解决办法
Found the solution myself in the end. The problem was not with the LinearLayout, but with the Scrol ...
- 【LR】关于宽带与比特之间的关系
1.比特=bps 2M比特的意思是2宽带 1M宽带=12M比特=1*1024K比特=1*1024*1024比特=1057648比特 2M宽带=2*1024*1024比特=2097152比特 4M宽带= ...
- 为Fitnesse-20140630定制RestFixture代码
摘要:Fitnesse插件RestFixture在最新版Fitnesse输出测试结果为html文本,而非html.本博文记录RestFixture定制代码的过程. 准备开发环境 假定你已经正确安装JD ...
- Linux时间与Windows差8个时区的问题解决方法
我的Debian7.1的时间与windows上的时间不一致,正好差8个时区,原因是Debian将机器的物理时间理解为UTC时间了.去网上找了好多文章,基本上都是说要改/etc/default/rcS, ...
- exists与in的使用与区别
1.in的使用举例 select * from tableA where id in (select id from tableB) 2.exists的使用举例 select * from table ...
- bzoj 1025 [SCOI2009]游戏(置换群,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1025 [题意] 给定n,问1..n在不同的置换下变回原序列需要的不同排数有多少种. [ ...
- Windows Azure 不能ping通的解决方案
Windows Azure 不能ping通如何解决? 为了避免Ping Flood攻击,Windows Azure不开放对外ICMP通讯协定,所以使用ping命令我们是无法ping通的.在微软资料中心 ...
- 机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA)
版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...
- Namespaces(命名空间)
datastore,Blobstore,memcache一起为应用存储数据.这对于在全球范围内分割数据是有用的.比如,一个应用可以为多个公司服务,每个公司可以看到它自己的隔离的应用实例,没有公司可以看 ...
- BAT-使用BAT方法结束进程(删除进程)
@echo off taskkill /f /im GAM.exe taskkill /f /im GCL10.exe