1
以下は私の作業コードです。これは、コンソールでエラーを表示せず、配列アイテムとテスト見出しを期待どおりに出力しません。しかし、どういうわけか、ダイナミックな背景スタイリングが働いていない、私がここで間違っていることを教えてください。動的なスタイリングがAngular2で機能しない
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>{{name}}</h1>
<div class="one" *ngFor="let item of colArr" style="background: {{item}};">{{item}}</div>
`,
styles: [`
.one {
width: 100%;
text-align: center;
padding: 10px;
margin: 5px;
}
`]
})
export class HomeComponent {
public name = 'test';
public colArr = ['#111111', '#222222', '#333333', '#444444', '#555555', '#666666', '#777777', '#888888', '#999999'];
}
後、私は取得しています出力される - style
に直接結合
Thxをを使用しますが、あなたの最初のソリューションは、 '[ngStyle] =動作していない「{背景:項目を、」'秒正常に動作しても1 –
私は逃しました最初の解決策では '}'です。 –
いいえ、このようにする必要があります - '[ngStyle] =" {background:item} "' 'それ以外の場合は'; 'のためコンパイルとエラーが発生しません –