[Angular Directive] Implement Structural Directive Data Binding with Context in Angular

Source: Internet
Author: User
Tags export class

Just like *ngFor in, you ' re able to pass in data into your own structural directives. This was done by declaring the variable using a let statement then passing context into the call createEmbeddedView .

We know *ngfor we do like this:

*ngfor="letmessage of messages"

So how can we also does like this?

Remember that

<*three= "Let messages"><!-- <  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 a point out after the ' three ' as an input.

<*three= "let message">{{message.to}}-{{message.message}}< /h2>

[Angular Directive] Implement Structural Directive Data Binding with Context in Angular

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.