Just like in *ngFor, you're able to pass in data into your own structural directives. This is done by declaring the variable using a let statement then passing context into the createEmbeddedView call.

We know *ngFor we did like this:

*ngFor="let message of messages"

So how can we also do like this?

Remember that

<h1 *three="let messages"> 

<!-- Equal to --> 

<template let-messages></template>
import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core";
@Directive({
selector: '[three]'
})
export class ThreeDirective {
@Input() set three(value) {
let num = 3; while (num--) {
const message = {
to: "People" + Math.random(),
message: "Hello" + Math.random()
};
this.view.createEmbeddedView(this.template, {
$implicit: message
})
}
} constructor(private template: TemplateRef<any>, private view: ViewContainerRef) { }
}

There to avoid change detection problem (value changed after compoennt inited). We need to point out after 'three' as an input.

<h2 *three="let message">{{message.to}} - {{message.message}}</h2>

[Angular Directive] Implement Structural Directive Data Binding with Context in Angular的更多相关文章

  1. angular学习(二)—— Data Binding

    转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...

  2. [Angular Directive] Write a Structural Directive in Angular 2

    Structural directives enable you to use an element as a template for creating additional elements. C ...

  3. [Angular] Step-By-Step Implementation of a Structural Directive - Learn ViewContainerRef

    For example we have two buttons: When we click nether one of those tow button, the modal should show ...

  4. [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2

     Just like passing in an array to *ngFor, you can pass in any value into your structural directive s ...

  5. Angular之 Scope和 Directive

    ---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...

  6. angular的GitHub Repository Directive Example学习

    angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...

  7. Angular自定义指令(directive)

    angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...

  8. angular自定义指令-directive

    Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...

  9. angularJS 中的two-way data binding.

    原文: https://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angularjs ---------- ...

随机推荐

  1. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  2. 常见ActiveX控件下载大全

    ActiveX是微软对于一系列策略性面向对象程序技术和工具的称呼,ActiveX控件可以在Windows窗体和Web程序上使用,所以不管是什么语 言开发的应用程序只要在windows窗体和html页面 ...

  3. ubuntu下的apache+php+mysql的安装

    平时我都时在windows下搭配apache+php+mysql环境的,只不过后来听别人说在linux下搭配apache+php+mysql更受欢迎,而且一般公司也是用这样的搭配,所以今天在试着在ub ...

  4. [tarjan] hdu 3836 Equivalent Sets

    主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...

  5. javascript 学习总结(九)面向对象编程

    1.面向对象的工厂方法 function createPerson(name, age, job){ var o = new Object(); o.name = name; o.age = age; ...

  6. Android项目---HtmlParse

    在解析网站上的内容的时候,总会出现很多html的标签,一般在遇到这种数据的时候,就可以用上Html 如: content.setText(Html.fromHtml("<html> ...

  7. 关于WCF的一些知识点

    首先,WCF和WebService一些区别1,WCF支持多中通信协议,http/https tcp/udp/msmq.命名管道,对等网,消息可达性,事物流等.2,WCF可以与ASP.NET集成,共享同 ...

  8. 学习GDI+ (1)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. IOS UI 第三篇:基本UI

    工厂模式:   .h文件:   #import <Foundation/Foundation.h>typedef enum{    QFRed,    QFYellow,    QFBlu ...

  10. sql 通过存储过程和自定义类型批量新增数据

    1,建立存储过程 create PROCEDURE [dbo].[p_Company_Insert] @CompanyCollection [CompanyTableType] READONLY AS ...