加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2 – 角度2.0绑定值到样式

发布时间:2020-12-17 08:23:27 所属栏目:安全 来源:网络整理
导读:我试图绑定一个颜色属性从我的类(通过属性绑定获取)来设置我的div的背景颜色。 import {Component,Template} from 'angular2/angular2';@Component({ selector: 'circle',bind:{ "color":"color" }})@Template({ url: System.baseURL + "/components/circle/
我试图绑定一个颜色属性从我的类(通过属性绑定获取)来设置我的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">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读