[Angular Directive] Implement Structural Directive Data Binding with Context in Angular
2017-01-16 19:18
489 查看
Just like in
We know *ngFor we did like this:
So how can we also do like this?
Remember that
There to avoid change detection problem (value changed after compoennt inited). We need to point out after 'three' as an input.
*ngFor, you're able to pass in data into your own structural directives. This is done by declaring the variable using a
letstatement then passing context into the
createEmbeddedViewcall.
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>
相关文章推荐
- Bug--Error creating bean with name 'sessionFactory' defined in class path resource [data_context.xml
- part 5 Two way databinding in AngularJS
- [Angular] Get started with data binding and SPA
- JUnit Test in Spring Context with JNDI Data Source
- [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
- [Angular Directive] Combine HostBinding with Services in Angular 2 Directives
- A for loop javascript event dynamically binding with customized data as paramete passed in test
- Performance Comparison Studies With Real-Life Scenarios in an Experimental Data Taking Context Lever
- JUnit Test in Spring Context with JNDI Data Source
- A for loop javascript event dynamically binding with customized data as paramete passed in test
- Writing A Structural Directive in Angular 2
- Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
- [Angular Directive] Write a Structural Directive in Angular 2
- Simplified and Extended Data Binding Syntax in ASP.NET 2.0
- Error creating bean with name 'sessionFactory' defined in ServletContext resource
- How to populate the datagrid on background thread with data binding by using Visual C#
- Putting Data and Business Process Integration in Context @ JDJ
- Building a Data Warehouse: With Examples in SQL Server (Hardcover), Dec.2007.eBook-BBL
- 开始正式的看 Working with Data in ASP.NET 2.0
- securely implement request processing, filtering and content redirection with HTTP pipeline in ASP.NET