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的更多相关文章

  1. Create a simple REST web service with Python--转载

    今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...

  2. [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 ...

  3. [Angular] Using directive to create a simple Credit card validator

    We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...

  4. [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 ...

  5. [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> ...

  6. [Angular 2] More on *ngFor, @ContentChildren & QueryList<>

    In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 【习题 7-8 UVA-12107】Digit Puzzle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大层数.(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字.. 一定要注意字典序问题. 每次优先 ...

  2. robot framework 使用三:浏览器兼容性自己主动化

    robot framework 測试浏览器兼容性 上图中黄色圈的地方默认什么都不写.是firefox浏览器,写上ie就是ie浏览器了 firefox最新版本号即可.ie须要设置: 1. IE选项设置的 ...

  3. 基于Redis bitmap实现开关配置功能

    作者:zhanhailiang 日期:2014-12-21 bitmap api SETBIT key offset value 对key所储存的字符串值,设置或清除指定偏移量上的位(bit). 位的 ...

  4. python-string中部分string替换

    今天遇到一个问题,就是需要把 “/home/zhangshuli/32_kk/” 中的32_kk 替换成为 52_kk 然后就在网上找方法,刚开始尝试的方法是 aaa = "/home/zh ...

  5. 最小二乘法,python3实现

    https://www.cnblogs.com/BlogOfMr-Leo/p/8627311.html                      [用的是库函数] https://blog.csdn. ...

  6. 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game

    [链接]点击打开链接 [题意] 水题 [题解] 枚举b从x..y看看k*i是不是在l..r之间就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <cstdio> ...

  7. 今天遇到奇怪的事:SVN本地代码的标记突然没了,Clean up也报错

    今天遇到奇怪的事:SVN本地代码的标记突然没了.Clean up也报错 脑子一想这样的情况,能够先把原来的文件夹改一个名字.又一次把代码check out下来,再合并提交更新,但这样也太LOW了吧 上 ...

  8. .netcore下的微服务、容器、运维、自动化发布

    原文:.netcore下的微服务.容器.运维.自动化发布 微服务 1.1     基本概念 1.1.1       什么是微服务? 微服务架构是SOA思想某一种具体实现.是一种将单应用程序作为一套小型 ...

  9. FTP 访问的形式

    主要是扼要的列举一下访问的方式,不涉及太具体的内容.大家可以在百度上搜索一下具体的操作方法. 主要有: 1. 网页浏览器中输入 ftp://192.168.0.111的形式. 2. 资源管理器中输入f ...

  10. C++卷积神经网络实例:tiny_cnn代码具体解释(8)——partial_connected_layer层结构类分析(上)

    在之前的博文中我们已经将顶层的网络结构都介绍完毕,包括卷积层.下採样层.全连接层,在这篇博文中主要有两个任务.一是总体贯通一下卷积神经网络在对图像进行卷积处理的整个流程,二是继续我们的类分析.这次须要 ...