【发布时间】:2019-02-04 00:26:02
【问题描述】:
有没有办法使用 renderer2 从指令中注入材质组件?
在我的例子中,我尝试制定一个指令,在 mat-button 中显示 mat-progress-spinner。
例如,这是一个指令。这段代码正在做类似于我正在尝试做的事情。由于我在我的项目中使用 Angular 材质,我想添加一个名为 mat-progress-spinner 的材质组件,而不是这个简单的 gif
@Directive({
selector: '[appButtonLoader]'
})
export class ButtonLoaderDirective {
img: HTMLImageElement;
@Input() set appButtonLoader(value: boolean) {
this.toggle(value);
}
constructor(private element:ElementRef) {
this.img = document.createElement('img');
this.img.src = 'https://www.winston.com/cached/40181/images/spinner-white.gif';
this.toggle(this.appButtonLoader);
}
toggle(condition: boolean) {
condition ? this.show() : this.hide()
}
show() {
this.element.nativeElement.appendChild(this.img);
}
hide() {
this.img.remove();
}
}
stackblitz:https://stackblitz.com/edit/load-button-directive?file=app%2Fbutton-loader.directive.ts
【问题讨论】:
-
为了提高您问题的可读性而进行的小改动。但我建议你自己尝试一下,而不是添加一些minimal reproducible example。与“这里有一个要求,怎么做”风格的问题相比,包含“自己的努力”的问题通常会收到更多有用的反馈。
-
问题已编辑
标签: angular-material