简体   繁体   English

Angular:包装器组件以传递内部指令/组件

[英]Angular: wrapper component to pass inner directives/components

I have 3 components: 我有3个组成部分:

(1) AppComponent : This is just a regular AppComponent , nothing special. (1) AppComponent :这只是一个常规的AppComponent ,没什么特别的。 In app.component.html I use my MyComponent : app.component.html中,我使用MyComponent

<my-component></my-component>

(2) MyComponent : This is just a wrapper component that contains TheirComponent like this ( my.component.html ): (2)MyComponent的 :这只是一个包含TheirComponent像这样(my.component.html)一个包装部件:

<their-component></their-component>

(3) TheirComponent : This is a third-party component (from an NPM package). (3)他们的组件 :这是第三方组件(来自NPM软件包)。 It can contain directives and other components from the third-party components. 它可以包含来自第三方组件的指令和其他组件。 For example: 例如:

<their-component>
    <their-header>Title</their-header>
    <their-footer>Title</their-footer>
</their-component>

My goal is to put all of these additional, inner directives and components ( their-header and their-footer in this example) in app.component.html , and pass it through MyComponent to TheirComponent . 我的目标是将所有这些附加的内部指令和组件(在此示例中their-headertheir-footer )放入app.component.html中 ,并将其通过MyComponent传递给themComponent So my app.component.html should look like this: 所以我的app.component.html应该看起来像这样:

<my-component>
    <their-header>Title</their-header>
    <their-footer>Title</their-footer>
</my-component>

I tried to use ng-content for this. 我试图为此使用ng-content It works with simple HTML elements, I know, but not with these third-party directives. 我知道,它只适用于简单的HTML元素,但不适用于这些第三方指令。 This is what I tried ( my.component.html ): 这是我尝试过的( my.component.html ):

<their-component>
    <ng-content></ng-content>
</their-component>

It doesn't work, and there are no errors. 它不起作用,并且没有错误。 Maybe this is a content projection problem, I'm not sure, how this is called. 也许这是一个内容投影问题,我不确定如何称呼它。 Any solution for this? 有什么解决办法吗? Thanks. 谢谢。


Example: https://plnkr.co/edit/SgM4sbYQrZXDcrKuon2d?p=preview 示例: https//plnkr.co/edit/SgM4sbYQrZXDcrKuon2d?p = preview

If you really need it you can take advantage of using ngProjectAs attribute. 如果确实需要,可以利用ngProjectAs属性。

In order to have all addition functionality provided by ButtonGroup ( k-group-start , k-group-end classes etc) you have to pass buttons array to kendo component. 为了具有ButtonGroup提供的所有附加功能( k-group-startk-group-end类等),您必须将button数组传递给kendo组件。

Here's how it could look like: 它看起来像这样:

import { Component, QueryList, ContentChildren, Input, ViewChild } from '@angular/core';
import { Button, ButtonGroup } from '@progress/kendo-angular-buttons';

@Component({
  selector: 'my-component',
  template: `
    <kendo-buttongroup>
        <ng-content ngProjectAs="[kendoButton]"></ng-content>
    </kendo-buttongroup>
  `
})
export class MyComponent {

  @ViewChild(ButtonGroup) buttonGroup: ButtonGroup;

  @ContentChildren(Button) buttons: QueryList<Button>;

  ngAfterViewInit() {
    this.buttonGroup.buttons = this.buttons;
    this.buttonGroup.ngAfterContentInit();
  }
}

Plunker Example 柱塞示例

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM