반응형
스타일에 대한 Angular 2.0 바인딩 값
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 클래스 내부의 색상 속성 변경을 어떻게 볼 수 있습니까? 대체 무엇입니까
$scope.$watch("color", function(a,b,){});
각도 2.0에서?
문자열에 대한 스타일 바인딩이 작동하지 않는 것으로 나타났습니다. 해결책은 스타일의 배경을 바인딩하는 것입니다.
<div class="circle" [style.background]="color">
현재 (2017 년 1 월 / Angular> 2.0) 현재 다음을 사용할 수 있습니다.
changeBackground(): any {
return { 'background-color': this.color };
}
과
<div class="circle" [ngStyle]="changeBackground()">
<!-- <content></content> --> <!-- content is now deprecated -->
<ng-content><ng-content> <!-- Use ng-content instead -->
</div>
가장 짧은 방법은 다음과 같습니다.
<div class="circle" [ngStyle]="{ 'background-color': color }">
<!-- <content></content> --> <!-- content is now deprecated -->
<ng-content><ng-content> <!-- Use ng-content instead -->
</div>
다음과 같이 alpha28과 함께 작동하도록 관리했습니다.
import {Component, View} from 'angular2/angular2';
@Component({
selector: 'circle',
properties: ['color: color'],
})
@View({
template: `<style>
.circle{
width:50px;
height: 50px;
border-radius: 25px;
}
</style>
<div class="circle" [style.background-color]="changeBackground()">
<content></content>
</div>
`
})
export class Circle {
color;
constructor(){
}
changeBackground(): string {
return this.color;
}
}
and called it like this <circle color='yellow'></circle>
Try [attr.style]="changeBackground()"
In your app.component.html use:
[ngStyle]="{'background':backcolor}"
In app.ts declare variable of string type
backcolor:string
.Set the variable
this.backcolor="red"
.
참고URL : https://stackoverflow.com/questions/29515475/angular-2-0-binding-value-to-style
반응형
'code' 카테고리의 다른 글
Golang-채널 버퍼 크기는 무엇입니까? (0) | 2020.10.21 |
---|---|
Promise 배열을 순차적으로 실행하려면 어떻게해야합니까? (0) | 2020.10.21 |
Count 속성 대 Count () 메서드? (0) | 2020.10.21 |
MongoDB Aggregation : 총 레코드 수를 얻는 방법은 무엇입니까? (0) | 2020.10.20 |
플래시 메시지가 사라지지 않는 이유는 무엇입니까? (0) | 2020.10.20 |