Javascript 角度9:ngOnChanges未为属性触发指令的方法
我有一个设置svg路径的指令Javascript 角度9:ngOnChanges未为属性触发指令的方法,javascript,angular,angular-directive,Javascript,Angular,Angular Directive,我有一个设置svg路径的指令 @Directive({ selector: 'svg use' }) export class SvgDirective implements OnChanges { @Input('attr.xlink:href') icon: string; constructor(private el: ElementRef) { const value = this.el.nativeElement.getAttribute('xlink:href'
@Directive({
selector: 'svg use'
})
export class SvgDirective implements OnChanges {
@Input('attr.xlink:href') icon: string;
constructor(private el: ElementRef) {
const value = this.el.nativeElement.getAttribute('xlink:href');
if (value) {
setTimeout(() => {
this.el.nativeElement.setAttribute('xlink:href', 'assets/svg-sprite.svg' + value);
});
}
}
ngOnChanges(changes: SimpleChanges): void {
if (this.icon) {
setTimeout(() => {
this.el.nativeElement.setAttribute('xlink:href', 'assets/svg-sprite.svg' + this.icon);
});
}
}
}
现在,理想情况下,任何指定了该指令且输入数据被传递给该指令的元素,都应该调用ngOnChanges
<svg viewBox="0 0 100 100" class="icon">
<use xlink:href="filterApplied ? '#filter-filled' : '#filter'""></use>
</svg>