2017-10-01 3 views
2

私は角度4で少し新しいですので、これはちょっとばかげた質問かもしれません..私の値はphotourl:string;です。私のfirebaseデータベースからの画像のURLを含んでいます..そして、私はこのURLを私のcomponent.htmlにある背景画像として使用したいので、{{photourl}}は私のcomponent.html {{photourl }}私のcomponent.cssで認識されません..私は直接私のcomponent.cssファイルでtypescriptの値を使用することができますか、またはcomponent.cssに介入するcomponent.tsからjavascriptを使用する必要がありますか?角4はsomething.component.cssにsomething.component.tsからの値を使用します

答えて

1

ngStyleディレクティブを使用してください。

<my-component [ngStyle]="{ 'background-image': photoUrl }"></my-component> 

出典: `高さ:photoUrl`は、私はまた、これらの属性がしたい:https://angular.io/api/common/NgStyle

+0

まあ、私はちょうど'「背景画像」を持つようにしたいいけない400ピクセルを。 ボーダー:1pxソリッド#129CF3; 背景色:黒; background-repeat:no-repeat; background-attachment:fixed; background-size:cover; -moz-background-size:cover; -o-background-size:cover; -webkit-background-size:cover; バックグラウンドポジション:50%50%;「[ngStyle]でこれをどうすればできますか? – Zekelonas

+0

また、 '[ngStyle] =" {'background-image':photoUrl} ''は動作しません:/それを動作させる方法が見つからない '[ngStyle] =" {'background-image': 'url (photoUrl) '​​} "、" [ngStyle] = "{'背景画像 ':url(photoUrl)}' 'どちらも動作しません.. – Zekelonas

関連する問題