【发布时间】:2020-09-04 07:45:33
【问题描述】:
我有一个组件 HelloComponent。该组件采用 MyButtonDirective 类型的所有内容子项,并为每个子项呈现一个按钮标记。
@Component({
selector: 'hello',
template: `
<p>{{buttons.length}}</p>
<button *ngFor="let b of buttons" (click)="b.onClick()">{{b.text}}</button>`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent {
@ContentChildren(MyButtonDirective, {descendants: true}) buttons: QueryList<MyButtonDirective>;
}
@Directive({
selector: 'my-button'
})
export class MyButtonDirective {
@Input() text: string;
@Output() click: EventEmitter<any> = new EventEmitter<any>();
onClick() {
console.log('my-button clicked');
this.click.emit();
}
}
有没有一种方法可以子类化或从 MyButtonDirective 派生来创建一个新的指令 och 组件,该组件将包含在 HelloComponent 的 contentchildren 中?
<hello>
<my-button></my-button>
<my-button></my-button>
<extra-button></extra-button> <-- How can this be included in contenchildren of Hello?
</hello>
我为此创建了一个Stackblitz。
【问题讨论】:
标签: angular