Angular Style

16 篇文章 0 订阅

样式穿透

:host /deep/ //  当前组件和其子组件内部样式生效
/deep/ 组合器还有两个别名:>>>  ::ng-deep。
encapsulation可选值为 Emulate(默认) | Native | None(可进可出) | ShadowDom

@Component({
  selector: 'fx-button',
  encapsulation: ViewEncapsulation.None, // 指定组件的样式封装性
  templateUrl: './fx-button.component.html',
  styleUrls: ['./fx-button.component.scss']
})

一、[style.propetyName]


1.基本用法

<p [style.fontSize]='fSize'></p> 
<p [style.width.px]='width200'></p> //带单位
fSize: string='14px’; 
width200: string='200';

2.绑定函数

<p [style.height]="setHeight(list.length)"></p>
setHeight(length) {
  if(length==1){
      return '4rem';
  }
  else if(length==2){
      return '6rem';
  }
  else{
      return '10rem';
  }
}

二、[ngStyle]


1.基本用法

<p [ngStyle]="eleStyle"></p>
eleStyle: any={ 
   fontSize:'14px’; 
   color:'#333’; 
 }

2.对象型写法

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

3.对象判断写法

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

三、[class.className]


1.基本用法

<p [class.changeColor]="flag"></p>
flag: boolean=true
changeColor: { color:'#fff'; }

四、[ngClass]


1.基本用法

// string - 会把列在字符串中的 CSS 类(空格分隔)添加进来

<p [ngClass]="'first second'"></p> 

<p [ngClass]="stringExp|arrayExp|objExp"></p>
// Array - 会把数组中的各个元素作为 CSS 类添加进来

<p [ngClass]="['first', 'second']"></p>
// Object - 每个 key 都是要处理的 CSS 类,当表达式求值为真的时候则添加,为假则移除。

.title{ color: '#333'; }

<p [ngClass]="{'title':true}"></p> //第一个参数是类名,第二个参数是boolean值

<p [ngClass]="{‘title class1 class2':true}"></p> //第一个参数是类名,第二个参数是boolean值

2.判断

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

3.函数用法

<p [ngClass]="getIco(menuCode)"></p>
getIco(menuCode){
   let className="";
   switch (menuCode)
   {
     case 'index':
       className= "index";
       break;
     case 'view':
       className= "view";
       break;
     case 'operation':
       className= "operation";
       break;
    };
   return className;
}

https://serious-lose.notion.site/NG-Style-798c99f25074457494d1df2341598515

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值