[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>
时间: 2024-11-08 07:47:05

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

[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

angular的GitHub Repository Directive Example学习

angular的GitHub Repository Directive Example学习 运行下面代码 <!DOCTYPE html><html ng-app="myApp"><head>     <meta charset="utf-8" />     <title>GitHub Repository Directive Example</title>       <script sr

Angular学习心得之directive——require选项的细节

谈require选项之前,应该先说说controller选项,controller选项允许指令对其他指令提供一个类似接口的功能,只要别的指令(甚至是自己)有需要,就可以获取该controller,将其作为一个对象,并取得其中的所有内容.而require就是连接两个指令的锁链,它可以选择性地获取指令中已经定义好的controller,并作为link函数的第四个参数传递进去,link函数的四个参数分别为scope,element,attr和someCtrl,最后一个就是通过require获取的con

详说Angular之指令(directive)

前言 angular核心部分如下图几大块,最重要的莫过于指令这一部分,本文将重点讲解指令这一部分,后续笔者将通过陆续的学习来叙述其他如:factory.service等,若有叙述错误之处,欢迎各位指正以及批评.本文将通过一些实例来进行叙述. 话题 restrict以及replace 在sublimeText中安装angular插件之后,我们需要创建指令时此时将自动出现如下定义:所以我们将重点放在如下各个变量的定义. .directive('', ['', function(){ // Runs

angular自定义指令-directive

Directive究竟是个怎么样的一个东西呢?我个人的理解是这样的:将一段html.js封装在一起,形成一个可复用的独立个体,具体特定的功能.下面我们来详细解读一下Directive的一般性用法. var myDirective = angular.module('directives', []); myDirective.directive('directiveName', function($inject) { return { template: '<div></div>',

angularJS 中的two-way data binding.

原文: https://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angularjs ------------------------------------------------------------------------------------------------------------ It turns out that there's a very elegant solution t

[Angular 2] Using ng-model for two-way binding

Two-way binding still exists in Angular 2 and ng-model makes it simple. The syntax is a combination of the [input] and (output) syntax to represent that the data is being pushed out and pulled in. import {Component} from 'angular2/core'; import {Todo

Data binding concepts in .NET windows forms

[转]Data binding concepts in .NET windows forms A detailed look at the concepts involved in data binding and controlling data binding. Example showing how to control DataBinding VB.NET - 22 Kb Example showing how to control DataBinding C# - 13 Kb Intr

完全掌握Android Data Binding

转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0603/2992.html 来源 https://github.com/LyndonChin/MasteringAndroidDataBinding 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 本教程是跟着 Data Bind