Angular2样式绑定

1、[style.propetyName]

a. 基本用法

//template
<p [style.fontSize]='fSize'></p>
<p [style.width.px]='width200'></p>    //带单位

//component.ts
fSize: string='14px';
width200: string='200';

b. 绑定函数

//template
<p [style.height]="setHeight(list.length)"></p>   

 //component.ts
setHeight(length) {
  if(length==1){
      return '4rem';
  }
  else if(length==2){
      return '6rem';
  }
  else{
      return '10rem';
  }
}

2、[ngStyle]

a. 基本用法

//template
<p [ngStyle]="eleStyle"></p>

//component.ts
eleStyle: any={
      fontSize:'14px';
      color:'#333';
 }

b. 对象型写法

<p [ngStyle]="{'background': '#ff0'}"></p>   

c. 判断添加

<p [ngStyle]="{'background': userName=='xjy'?'red':'green'}"></p>     

3、[class.className]

//template
<p [class.changeColor]="flag"></p>

//component.ts
flag: boolean=true

//css
changeColor: { color:'#fff'; }

4、[ngClass]

a. 基本用法

//template 
<p [ngClass]="{'title':true}"></p>   //第一个参数是类名,第二个参数是boolean值
//css
.title{ color: '#333'; }

b. 判断

//template
<p [ngClass]="{'bgColor': userName=='xjy' }"></p>
//css
.bgColor{ background: '#f00'; }

c. 绑定函数

//template 
<p [ngClass]="getIco(menuCode)"></p>
//component.ts
getIco(menuCode){
   let className="";
   switch (menuCode)
   {
     case 'index':
     className= "index";
     break;
     case 'view':
     className= "view";
     break;
     case 'operation':
     className= "operation";
     break;
    };
   return className;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值