角度:将零部件传递给零部件 [英] Angular: Pass Component to a Component
问题描述
我有这个小的gridComponent:
I have this small gridComponent:
@Component({
selector: 'moving-grid',
templateUrl: './grid.component.html',
styleUrls: ['./grid.component.css']
})
export class GridComponent {
@Input('widgets') extComponents: Array<Component>;
}
和第二个testComponent
And a second testComponent
@Component({
selector: 'test',
template: `
<div #content>I say hello<li>i</li><li>u</li><button (click)="test()">click me</button> world</div>
`
})
export class TestComponent {
@ViewChild('content') content: HTMLElement;
getContent() {
return this.content;
}
test() {
console.log("test");
}
}
现在,我正在尝试将testComponent的多个实例传递给gridComponent.因此,我有第三个组件,看起来像这样:
Now I'm trying to pass multiple instances of testComponent to the gridComponent. Therefore I have a third Component which looks like this one:
template: `
<moving-grid [widgets]="[z1,z2]">
<test class="grid-item-content-001" #z1></test>
<test class="grid-item-content-002" #z2></test>
</moving-grid>
`
直到这一点,一切都按预期工作.但是,如何在gridComponent中从@Input渲染组件? 我的第一种方法是在testComponent中声明一个@ViewChild,并使用getContent()函数将其返回.但这是行不通的.我可以通过某种方式使用ng-content指令还是有更好的解决方案?
Until this point, everything works like expected. But how can I render the Components from @Input in the gridComponent? My first approach was to declare a @ViewChild in the testComponent and return it with a getContent()-function. But it won't work. Can I use the ng-content directive in some way or is there a better solution?
GridComponent看起来像这样.我想在其中一个黑框内显示@ Input-Component的模板.有可能吗?
The GridComponent looks like this. I want to display the templates of a @Input-Component inside one of the black boxes. Is it possible?
感谢任何帮助
推荐答案
您不应使用@Input
传入组件.为此,您可以使用 @ContentChildren
抽象WidgetComponent
:
You should not use an @Input
to pass in the components. You can use @ContentChildren
for that and an abstract WidgetComponent
:
@Component({
selector: 'moving-grid',
template: `
<div class="widget-wrapper">
<ng-container *ngFor="let widget of widgets">
<!-- use a ngSwitchCase here for different types-->
<grid-test-widget [widget]="widget" *ngIf="widget.active && widget.type === 'test'"></grid-widget>
</ng-container>
</div>
`,
styleUrls: ['./grid.component.css']
})
export class GridComponent implements AfterContentInit {
@ContentChildren('widget') widgets: WidgetComponent[];
ngAfterContentInit() {
//your components will be available here
}
}
第三个组件的模板将保持不变,但是没有[widgets]
和添加的#widget
名称:
The template of your third component will stay the same, but without the [widgets]
and an added #widget
name:
<moving-grid>
<test-widget class="grid-item-content-001" #widget [active]="false"></test>
<test-widget class="grid-item-content-002" #widget></test>
</moving-grid>
您的TestWidgetComponent
将扩展一个抽象的WidgetComponent:
Your TestWidgetComponent
which will extend an abstract WidgetComponent :
@Directive({
selector: 'test-widget'
})
export class TestWidgetComponent extends WidgetComponent {
public type: string = 'test';
}
和您的WidgetComponent
:
@Injectable()
export class WidgetComponent {
@Input()
public active: boolean;
public type: string;
}
然后您将基于窗口小部件的类型拥有几个网格窗口小部件:
And you'll have several grid widgets based on the type of the widget:
@Component({
selector: 'grid-test-widget',
template: `<div #content>I say hello<li>i</li><li>u</li><button (click)="test()">click me</button> world</div>`
})
export class GridTestWidgetComponent{}
这篇关于角度:将零部件传递给零部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!