[Angular Directive] Implement Structural Directive Data Binding with Context in Angular
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的更多相关文章
- angular学习(二)—— Data Binding
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...
- [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 ...
- [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 ...
- [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 ...
- Angular之 Scope和 Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- angular的GitHub Repository Directive Example学习
angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...
- Angular自定义指令(directive)
angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...
- angular自定义指令-directive
Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. v ...
- angularJS 中的two-way data binding.
原文: https://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angularjs ---------- ...
随机推荐
- 使用JasperReport+iReport进行Web报表开发
使用JasperReport+iReport进行Web报表开发 前言 在实际工程中非常,报告是其中很重要的一部分,结果以报表的形式呈现出来.这里所提到的报表可不是简单的二维表,而是拥有复杂表头的.多维 ...
- IEnumerable<T>转换为IList<SelectListItem>
扩展方法IEnumerable<T>转换为IList<SelectListItem> ,提供@Html.DropDownList使用 由于在MVC中经常会使用到@Html. ...
- sql汉字转拼音
/*创建取拼音首字母函数*/ create function [dbo].[fn_ChineseToSpell](@strChinese varchar(500)='') returns varcha ...
- 实现透明渐变的Activity
如果光是透明全屏的Activity的话,直接继承内置theme即可 <activity android:theme="@android:style/Theme.NoTitleBar.F ...
- 动态代理 原理简析(java. 动态编译,动态代理)
动态代理: 1.动态编译 JavaCompiler.CompilationTask 动态编译想理解自己查API文档 2.反射被代理类 主要使用Method.invoke(Object o,Object ...
- Gitlab,Github与Bitbucket
这段时间开始做毕设,决定使用git来管理代码和相关的文档. 同时希望有一个远程托管,决定在github.bitbucket,以及我自己搭建的gitlab服务器中间选一个,最终决定使用bitbuckt. ...
- 【转载】matlab如何判断一个点是否在多面体内
转载自:http://www.52souji.net/point-within-a-polyhedron/ 我遇到的一个实际问题是:要在空位区域随机放置一定数量的原子,这些原子在空位区域任何一处存在的 ...
- C# 读取 vCard 格式
办公室里有时忙起来,会频繁进入这样一个循环,想找某个人的电话-去找名片-找不到名片-去查看手机-手机按解锁开关-手机滑屏/指纹/密码/图形解锁-手机按通话按键-输入那个人姓名的部分-找到电话-输入到P ...
- c语言,string库函数strstr实现
说明: 原型:char *strstr(char *haystack, char *needle); 用法:#include <string.h> 功能:从字符串haystack中寻找ne ...
- js中的AMD规范
回首萧瑟,残月挂角,孤草弄影. 看了一下上一篇随笔的日期,距离上一篇日志又过去了许久.在这段时间中,我尽全力去拯救那间便利店,可惜到最后依然失败,这一次是所有的出路全部没有了,我也做了所有的努力.闲下 ...