クラスのcolorプロパティ(属性バインディングによって取得)をバインドして、background-colorのを設定しようとしていdivます。
import {Component, Template} from 'angular2/angular2';
@Component({
selector: 'circle',
bind:{
"color":"color"
}
})
@Template({
url: System.baseURL + "/components/circle/template.html",
})
export class Circle {
constructor(){
}
changeBackground():string{
return "background-color:" + this.color + ";";
}
}
私のテンプレート:
<style>
.circle{
width:50px;
height: 50px;
background-color: lightgreen;
border-radius: 25px;
}
</style>
<div class="circle" [style]="changeBackground()">
<content></content>
</div>
このコンポーネントの使用法:
<circle color="teal"></circle>
私のバインディングは機能していませんが、例外もスローしません。
{{changeBackground()}}テンプレートのどこかに置くと、正しい文字列が返されます。
では、なぜスタイルバインディングが機能しないのですか?
また、Circleクラス内のcolorプロパティの変更をどのように監視しますか?の代替品は何ですか
$scope.$watch("color", function(a,b,){});
Angular 2では?
<div class="circle" [style.background]="'color'">