我需要通過@HostBinding
創建一個Angular 2+(我是4.x)指令(不是組件),它增加了一個背景圖像。我知道我很接近,但是當我檢查它時,我在Chrome的檢查器中看到background-image: none
。如何使用@HostBinding將背景圖像添加到Angular 2+指令?
import { Directive, HostBinding } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';
@Directive({
selector: '[myDirectiveWithBackgroundImage]'
})
export class MyDirective {
@HostBinding('style.background-image')
public backgroundImage: SafeStyle;
constructor(private sanitizer: DomSanitizer) {
this.backgroundImage = this.sanitizer.bypassSecurityTrustStyle(
'url(assets/images/favicon16.png) no-repeat scroll 7px 7px;'
);
}
}
我的資產/ images/favicon.16png由webpack提供服務。我已經試過各種路徑選項,/assets
,~/assets
,等等...但background-image
總是none
https://plnkr.co/edit/5w87jGVC7iZ7711x7qWV?p=preview
你可以創建一個plunker嗎? –
其中是/資產相對於/ src? – Vega
@Maximus https://plnkr.co/edit/5w87jGVC7iZ7711x7qWV?p=preview 但是,在plunkr中,它不會在所有**處添加'background-image ** **。它確實添加了其他樣式。 – jkyoutsey