[Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor.
What it should looks like in HTML?
<div>
<ul>
<li *myFor="let item of items; let i = index;">
{{ i }} Member: {{ item.name | json }}
</li>
<template myFor [myForOf]="items" let-item let-i="index">
<li>{{ i }} Member: {{ item.name | json }}</li>
</template>
</ul>
</div>
So here, we have a '*myFor' directive. It also use 'myForOf' direcitve. And a implicit value 'item'.
Notice that:
<li *myFor="let item of items; let i = index;">
{{ i }} Member: {{ item.name | json }}
</li>
and
<template myFor [myForOf]="items" let-item let-i="index">
<li>{{ i }} Member: {{ item.name | json }}</li>
</template>
They have the same effect.
Now let's create myFor directive:
import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core';
@Directive({
selector: '[myFor][myForOf]'
})
export class MyForDirective {
@Input()
set myForOf(collection) {
this.view.clear();
collection.forEach((item, index) => {
this.view.createEmbeddedView(
this.template,
{
$implicit: item,
index
}
)
});
}
constructor(
private view: ViewContainerRef,
private template: TemplateRef<any>
) {
// this.template will point to the host element
}
}
It is good to clear the view every time we generate new embedded view:
this.view.clear();
Also we gave implicit 'value' and 'index'.
this.view.createEmbeddedView(
this.template,
{
$implicit: item, // let-item
index // let-i="index"
}
)
[Angular] Create a simple *ngFor的更多相关文章
- Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...
- [Angular 2] Create a simple search Pipe
This lesson shows you how to create a component and pass its properties as it updates into a Pipe to ...
- [Angular] Using directive to create a simple Credit card validator
We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...
- [Angular 2] A Simple Form in Angular 2
When you create a Form in Angular 2, you can easily get all the values from the Form using ControlGr ...
- [Angular] Create a custom pipe
For example we want to create a pipe, to tranform byte to Mb. We using it in html like: <div> ...
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...
- [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 ...
- CHtmlEditCtrl(1) : Use CHtmlEditCtrl to Create a Simple HTML Editor
I needed a lightweight HTML editor to generate "rich text" emails, so I decided to explore ...
- [Angular] Create a custom validator for reactive forms in Angular
Also check: directive for form validation User input validation is a core part of creating proper HT ...
随机推荐
- 洛谷 P2562 [AHOI2002]Kitty猫基因编码
P2562 [AHOI2002]Kitty猫基因编码 题目描述 小可可选修了基础生物基因学.教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由 ...
- hdu 4932
枚举差和差的1/2 #include <cstdio> #include <cstring> #include <algorithm> using namespac ...
- 常用加密算法的Java实现总结(二)
常用加密算法的Java实现总结(二) ——对称加密算法DES.3DES和AES 摘自:http://www.blogjava.net/amigoxie/archive/2014/07/06/41550 ...
- POJ 2536 Gopher II (ZOJ 2536) 二分图匹配
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1882 http://poj.org/problem?id=2536 题目大 ...
- x=min(x, y)
x = min(x, y); ⇒ 当然 y 会有多个值传递进来 minHeight = min(minHeight, h[i]); 置于循环之中,不断将当前得到的最小高度值和新加入进来的值进行比较: ...
- CSU1656: Paper of FlyBrother(后缀数组)
Description FlyBrother is a superman, therefore he is always busy saving the world. To graduate fro ...
- 切换根控制器UIApplication 主屏幕UIScreen 读取文件资源NSBundle
//主屏幕设为webView CGRect frame = [UIScreen mainScreen].applicationFrame; UIWebView *webView = [[[UIWebV ...
- [Nuxt] Update State with Vuex Actions in Nuxt.js
You can conditionally add classes to Vue.js templates using v-bind:class. This will help display the ...
- Java中字节与对象之间的转换
近期公司里面用到了消息队列,而正如我们知道的是消息队列之间的是通过二进制形式的.以下就分享一下java中字节与对象之间的转换. 主要是用到了ByteArrayOutputStream和ObjectOu ...
- js私有变量
js私有变量 一.总结 1.在js函数中定义 this.name='张三'; (函数的属性)外部是可以访问的,但是 var name='张三'; (函数的私有变量),这样定义的话外部没有办法访问 2. ...