在 angular2 中构建包装器指令(包装一些内容/组件)
[英] Building a wrapper directive (wrap some content / component) in angular2
本文介绍了在 angular2 中构建包装器指令(包装一些内容/组件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Angular2 的新构建指令.我想要的是创建一个弹出指令,用一些 css 类包装内容.
内容
内容可以是纯文本和标题,例如:
<h2>标题</h2>内容要放在这里.
然后我想给它一个指令属性,如:popup
<h2>标题</h2>内容要放在这里.
指令应该做的是将 div 包裹在里面,让我们说:
<div class="其他类"><div class="数据"><h2>标题</h2>内容要放在这里.
到目前为止我描述的情况,这是一个属性还是结构指令.
import { Directive, ElementRef, HostListener, Input } from '@angular/core';@指示({选择器:`[弹出]`})导出类 PopupDirective {}
解决方案
另一个答案是相关但不同的.
有关更详细的信息,请参阅:如何有条件地在 ng-content 周围包裹一个 div - 我的解决方案是针对 Angular 4,但链接的问题有一些关于这对于 Angular 2 可能如何可行的提示.
我用一个组件和一个指令组合解决了这个问题.我的组件看起来像这样:
import { Component, Input, TemplateRef } from '@angular/core';@成分({选择器:'我的包装容器',模板:`<div class="whatever"><ng-container *ngTemplateOutlet="template"></ng-container>
`})导出类 WrapperContainerComponent {@Input() 模板:TemplateRef;}我的指令是这样的:
import { Directive, OnInit, Input, TemplateRef, ComponentRef, ComponentFactoryResolver, ViewContainerRef } from '@angular/core';@指示({选择器:'[myWrapperDirective]'})导出类 WrapperDirective 实现 OnInit {私有包装容器:ComponentRef;构造函数(私有模板引用:模板引用<任何>,私有 viewContainerRef: ViewContainerRef,私有 componentFactoryResolver: ComponentFactoryResolver) { }ngOnInit() {const containerFactory = this.componentFactoryResolver.resolveComponentFactory(WrapperContainerComponent);this.wrapperContainer = this.viewContainerRef.createComponent(containerFactory);this.wrapperContainer.instance.template = this.templateRef;}}
为了能够动态加载您的组件,您需要将您的组件列为 entryComponent
在你的模块中:
@NgModule({进口:[CommonModule],声明: [WrapperContainerComponent, WrapperDirective],出口:[WrapperContainerComponent,WrapperDirective],entryComponents: [WrapperContainerComponent]})导出类 MyModule{}
所以最后的 HTML 是:
呈现为:
<div class="whatever"><some_tag/>
</my-wrapper-container>
I'm pretty new building directives with Angular2. What I want is to create a popup directive that will wrap the content with some css classes.
Content
Content can be pure text and headers like:
<div class="data">
<h2>Header</h2>
Content to be placed here.
</div>
Then I want to give this a directive attribute like: popup
<div class="data" popup>
<h2>Header</h2>
Content to be placed here.
</div>
What the directive should do, is to wrap the div inside, lets say:
<div class="some class">
<div class="some other class">
<div class="data">
<h2>Header</h2>
Content to be placed here.
</div>
</div>
</div>
The case i described so far, is this a attribute or structural directives.
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: `[popup]`
})
export class PopupDirective {
}
解决方案
The other answer is related but different.
For something closer, see this: How to conditionally wrap a div around ng-content - my solution is for Angular 4, but the linked question has some hints about how this might be doable for Angular 2.
I solved this problem with a component and a directive combined. My component looks something like this:
import { Component, Input, TemplateRef } from '@angular/core';
@Component({
selector: 'my-wrapper-container',
template: `
<div class="whatever">
<ng-container *ngTemplateOutlet="template"></ng-container>
</div>
`
})
export class WrapperContainerComponent {
@Input() template: TemplateRef<any>;
}
and my directive like this:
import { Directive, OnInit, Input, TemplateRef, ComponentRef, ComponentFactoryResolver, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[myWrapperDirective]'
})
export class WrapperDirective implements OnInit {
private wrapperContainer: ComponentRef<WrapperContainerComponent>;
constructor(
private templateRef: TemplateRef<any>,
private viewContainerRef: ViewContainerRef,
private componentFactoryResolver: ComponentFactoryResolver
) { }
ngOnInit() {
const containerFactory = this.componentFactoryResolver.resolveComponentFactory(WrapperContainerComponent);
this.wrapperContainer = this.viewContainerRef.createComponent(containerFactory);
this.wrapperContainer.instance.template = this.templateRef;
}
}
To be able to load your component dynamically, you need to list your component as an entryComponent
inside your module :
@NgModule({
imports: [CommonModule],
declarations: [WrapperContainerComponent, WrapperDirective],
exports: [WrapperContainerComponent, WrapperDirective],
entryComponents: [WrapperContainerComponent]
})
export class MyModule{}
so the HTML in the end is:
<some_tag *myWrapperDirective />
Which renders as:
<my-wrapper-container>
<div class="whatever">
<some_tag />
</div>
</my-wrapper-container>
这篇关于在 angular2 中构建包装器指令(包装一些内容/组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!