[Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
2017-01-16 19:44
357 查看
Just like passing in an array to
The syntax looks like:
For template it would looks like:
It combimes 'three' and 'from' keywords.
So the directive would looks like:
*ngFor, you can pass in any value into your structural directive so that it can render templates based on those values. It's crucial to understand how the
*directivesyntax expands into a
<template>and adds a custom
@Inputbased on the syntax you use so that you can use your own data.
The syntax looks like:
<h2 *three="let message from messages">{{message.to}} - {{message.message}}</h2>
For template it would looks like:
<template [threeFrom]="messages"></template>
It combimes 'three' and 'from' keywords.
So the directive would looks like:
import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core"; @Directive({ selector: '[three]' }) export class ThreeDirective { @Input() set threeFrom({one, two, three}) {this.view.createEmbeddedView(this.template, { $implicit: { to: "People" + Math.random(), message: two } }); this.view.createEmbeddedView(this.template, { $implicit: { to: "People" + Math.random(), message: three } }); this.view.createEmbeddedView(this.template, { $implicit: { to: "People" + Math.random(), message: one } }); } constructor(private template: TemplateRef<any>, private view: ViewContainerRef) { } }
相关文章推荐
- [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
- 走进AngularJs(一)angular基本概念的认识与实战
- AngularJS ng-repeat下使用ng-model
- angular+ionic 的app上拉加载更新数据实现方法
- angularJS自定义directive之带参方法传递
- 使用AngularJS 跨站请求如何解决jsonp请求问题
- angularjs 1.x 阻止事件冒泡
- 简介AngularJS中使用factory和service的方法
- AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转
- angularjs的懒加载
- Angular项目构建指南 - 不再为angular构建而犹豫不决
- angularJS中repeat数据不能出现重复项解决
- Ionic2 问题解决汇总
- AngularJS最理想开发工具WebStorm
- Angular企业级开发(1)-AngularJS简介
- angular 函数表达式执行两次的问题
- 结构-行为-样式 - Angularjs 环境下Ztree结合JqueryUI实现拖拽
- Angular2 + NativeScript 跨平台开发笔记(一)
- angular 格式化日期
- AngularJS1.6版本中ui-router路由中/#!/的解决方法