[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 so that it can render templates based on those values. It‘s crucial to understand how the *directive syntax expands into a <template> and adds a custom @Input based 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) {

  }
}
时间: 2024-12-28 11:44:46

[Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2的相关文章

《Getting MEAN with Mongo Express Angular and Node》---ch08 Adding Angular components to an Express application(向Express应用添加Angular组件)

This chapter covers(本章概要)■ Getting to know Angular(了解Angrular)■ Adding Angular to an existing page(向动态页面添加Angular)■ Filtering lists of data(过滤列表数据)■ Using an API for reading data(使用API读取数据)■ Some Angular jargon: controllers, scope, filters,directives

DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS

FIELD OF INVENTION This invention relates to computer graphics processing, and more specifically to computer graphics processing using two or more architecturally distinct graphics processors. BACKGROUND OF INVENTION Many computing devices utilize hi

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

[AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers

The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bindToController on the directive. Here is a blog about bindToController from thoughtramwhich explains in detail why bingToController comes into handy an

[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2

A @Directive is used to add behavior to elements and components in your application. This makes @Directives ideal for behaviors such as "tracking" which don't belong in a Component, but do belong as a behavior in your application. import {Direct

angular 自定义指令详解 Directive

在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足你的各种需求的指令. 本篇文章的参考来自  AngularJS权威指南 , 文章中主要介绍指令定义的选项配置 废话不多说,下面就直接上代码 //angular指令的定义,myDirective ,使用驼峰命名法 angular.module('myApp', []) .directive('myDi

Exploring Angular 1.3: Binding to Directive Controllers

原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angular1.2引入了新的语法 controllerAs, 它让scope更加清楚.干净, 让controller更加聪明. 在我们的Angular应用中使用controllerAs可以避免开发者经常遇到的一些问题. controllerAs 做为命名空间 让我们用代码说事,有两个controller如下

关于angular中函数的先后执行之我见(以及angular的小bug)

在js中,函数的先后执行 (1)在angular中假设有这个场景,对表单资料进行编辑,刚好这个表单有select选项需要从后台中获取,这个时候这个表单使用angular进行开发的时候的正确打开方式应该是 先加载select选项,在加载表单的对应内容(由于http是异步的,并不是单纯的把js顺序调整一下就可以的) 这时候可以使用angular自带的$q返回promise来控制函数运行, 如果函数中没有其他的异步,简单粗暴的使用$timeout来控制 (2)input[type=hidden]使用n

[Angular] Read Custom HTTP Headers Sent by the Server in Angular

By default the response body doesn’t contain all the data that might be needed in your app. Your server might return some special header which you have to read explicitly. In such case we can use the { observe: ‘response’} configuration of the Angula