1 需求

  在前端开发中通常需要动态对某些元素的样式进行动态设定,传统的CSS绑定分为CSS类绑定和Style样式绑定;在Angular中利用相关指令同样可以完成CSS类绑定和CSS样式绑定

2 内置指令

  在angular中指令是作用在特定的DOM元素上的,目的是用来扩展元素的功能,为元素添加新的功能;angular框架本身提供的指令就叫做内置指令,例如:NgClass、NgStyle、NgIf、NgFor、NgSwitch等,利用NgClass、NgStyle和Class指令来动态绑定CSS样式

  技巧01:angular中的指令的类型首字母都是大写,但是在运用到DOM元素时需要将首字母变成小写,例如

    

3 利用NgClass指令实现CSS类绑定

  利用NgClass同时绑定一个或者多个CSS类

  3.1 NgClass绑定格式

    [ngClass]="{ CSS类名01:布尔类型,CSS类名:布尔类型 }"

    [ngClass]="styleMethod()"

    坑01:styleMethod是一个自定义的方法,该方法返回的是一个对象,该对象的格式是: { CSS类名01:布尔类型,CSS类名:布尔类型 }

    技巧01:对象的键最好用引号括起来,因为有的字符如果不用引号括起来就会报错,例如 _

    技巧02:对象的键值如果有特殊字符就需要用引号括起来,否则会报错(PS: 最好都用引号括起来)

  3.2 实际例子01

    <p [ngClass]="{style01: true, style02: true}">
Donnot aim for your success if you really want it. Just stick to do what you love and believe in.
</p>
.style01 {
color: yellow;
font-size: 14px;
}
.style02 {
background-color: red;
}

  3.3 实际例子02

    <p [ngClass]="styleMethod01()">
100% Capri employees model clothing while walking through the Bal Harbour Shops in Miami,
Florida, U.S., on Friday, Jan. 26, 2018. While some malls are desperately seeking financial
lifelines, Bal Harbour Shops is planning a $400 million expansion.
</p>
  styleMethod01() {
const style = {
style01: true,
style02: true
};
return style;
}
.style01 {
color: yellow;
font-size: 14px;
}
.style02 {
background-color: red;
}

  

  3.4 效果展示

    

    

4 利用NgStyle指令实现Style样式绑定

  利用NgStyle可以同时绑定一个或者多个内联样式的绑定

  4.1 NgStyle绑定格式

    [ngStyle]="{ 内联样式名称01:样式值,内联样式名称02:样式值}"

    [ngClass]="styleMethod()"

    坑01:styleMethod是一个自定义的方法,该方法返回的是一个对象,该对象的格式是:{ 内联样式名称01:样式值,内联样式名称02:样式值 }

    坑02:内联样式名称最好用引号括起来,样式值必须用引号括起来

    坑03:样式名称最好和CSS样式书写的格式保持一致,虽然有的样式名称可以进行一些变化,例如,CSS的写法是font_size,在NgStyle中的写法可以指font_size也可以是fontSize,只不过写成font_size时必须用引号括起来否则会报错,因为font_size里面包含了特殊字符

  4.2 实例例子01

    <p [ngStyle]="{color: 'red', 'font-size': '24px'}">
Shame may restrain what law does not prohibit.
</p>

  4.3 实际例子02

    <p [ngStyle]="styleMethod02()">
Shame may restrain what law does not prohibit.
</p>
  styleMethod02() {
const style = {
'color': 'red',
'font-size': '24px'
};
return style;
}

  4.4 实际效果展示

    

    

5 利用Class指令实现单个CSS类绑定

  Class指令只能绑定要给CSS类

  5.1 Class绑定格式

    [class.样式类名称]=“布尔类型”

    [class.样式类名称]=“styleMethod()”

    坑01:styleMethod是一个自定义方法,该方法的返回值必须是一个布尔类型

  5.2 实际例子01

    <p [class.style03]="true">
you know that all there is desire, thought, boding and longing;
</p>
.style03 {
color: skyblue;
font-size: 30px;
}

  5.3 实际例子02

    <p [class.style03]="styleMethod03()">
you know that all there is desire, thought, boding and longing;
</p>
  styleMethod03() {
return true;
}
.style03 {
color: skyblue;
font-size: 30px;
}

  5.4 效果展示

    

    

6 本博客代码汇总

  6.1 HTML代码

<div class="panel panel-primary">
<div class="panel-heading">动态设置class的三种方式</div>
<div class="panel-body">
<h3>利用ngClass指令实现</h3>
<p [ngClass]="{'style01': 'true', 'style02': 'true'}">
Donnot aim for your success if you really want it. Just stick to do what you love and believe in.
</p>
<p [ngClass]="styleMethod01()">
100% Capri employees model clothing while walking through the Bal Harbour Shops in Miami,
Florida, U.S., on Friday, Jan. 26, 2018. While some malls are desperately seeking financial
lifelines, Bal Harbour Shops is planning a $400 million expansion.
</p>
<hr /> <h3>利用ngStyle指令实现</h3>
<p [ngStyle]="{color: 'red', 'font-size': '24px'}">
Shame may restrain what law does not prohibit.
</p>
<p [ngStyle]="styleMethod02()">
Shame may restrain what law does not prohibit.
</p>
<hr /> <h3>利用class指令实现</h3>
<p [class.style03]="true">
you know that all there is desire, thought, boding and longing;
</p>
<p [class.style03]="styleMethod03()">
you know that all there is desire, thought, boding and longing;
</p> </div>
<div class="panel-footer">2018-3-8 10:14:57</div>
</div>

  6.2 CSS代码

.style01 {
color: yellow;
font-size: 14px;
}
.style02 {
background-color: red;
} .style03 {
color: skyblue;
font-size: 30px;
}

  6.4 TypeScript代码

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-test-demo',
templateUrl: './test-demo.component.html',
styleUrls: ['./test-demo.component.scss']
})
export class TestDemoComponent implements OnInit { constructor() { } ngOnInit() {
} styleMethod01() {
const style = {
style01: true,
style02: true
};
return style;
} styleMethod02() {
const style = {
'color': 'red',
'font-size': '24px'
};
return style;
} styleMethod03() {
return true;
} }

Angular21 动态绑定CSS样式的更多相关文章

  1. Vue之动态绑定CSS样式

    demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  2. Vue 动态绑定CSS样式

    今天在做项目上遇见了一个需求,通过不能的进度类型展示不同的进度形态,进度形态通过背景色和背景色上的文字显示. 效果图: 由于Element UI版本我用的是2.5.4  使用进度条的话 就没有2.9. ...

  3. Vue 框架-05-动态绑定 css 样式

    Vue 框架-05-动态绑定 css 样式 今天的小实例是关于 Vue 框架动态绑定 css 样式,这也是非常常用的一个部分 首先说一下 动态绑定,相对的大家都知道静态绑定,静态绑定的话,直接加 cl ...

  4. css样式让input垂直居中

    css样式让input垂直居中 css代码: .div1{ border: 1px solid #CCC; width:1120px; height:40px; margin:auto; displa ...

  5. 深度理解CSS样式表,内有彩蛋....

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. js设置css样式.

    在js设置css样式做法 var obj = document.getElementById('div'); obj.style.width = '100px'; obj.style.height = ...

  7. CSS样式表

    CSS样式及属性 样式标的基本概念 样式表的分类 1.内联样式表 和html联合显示,控制精确,但可重用性差,冗余多. 例:<p style="font-size:14px;" ...

  8. 脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)

    问题描述: 由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用.但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生 ...

  9. jQuery所支持的css样式

    jQuery所支持的css样式 backgroundPosition borderWidth borderBottomWidth borderLeftWidth borderRightWidth bo ...

随机推荐

  1. web开发过程中关于路径问题的总结

    约束: 相对路径概念-./代表当前目录.../代表上级目录 示例的所有文件都基于http://127.0.0.1:8080/test路径开放,test为对应的contextPath 前端 HTML标签 ...

  2. 2018/1/28 RocketMq学习笔记

    RocketMq是支持Topic模式的MQ中间件,它的传输格式为topic(主题,一个product对应一个主题,),Tag(标签,其实就是副标题,是为了更好的支持集群模式而出现的,这样客户端可以指定 ...

  3. Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录

    打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...

  4. php.ini 中文详解

    [PHP]  ; PHP还是一个不断发展的工具,其功能还在不断地删减  ; 而php.ini的设置更改可以反映出相当的变化,  ; 在使用新的PHP版本前,研究一下php.ini会有好处的   ;;; ...

  5. Centos 7系统优化脚本

    脚本如下,后续继续优化 #!/bin/bash #author junxi by #this script is only for CentOS 7.x #check the OS platform= ...

  6. Docker Centos6 下建立 Docker 桥接网络

    cd /etc/sysconfig/network-scripts/; cp ifcfg-eth0  ifcfg-br0 vi ifcfg-eth0 //增加BRIDGE=br0,删除IPADDR,N ...

  7. ansible实践2-拷贝文件或目录

      ansible testhost -m copy -a "src=/etc/ansible  dest=/tmp/ansibletest owner=root group=root mo ...

  8. [转]【C#】分享一个弹出浮动层,像右键菜单那样召即来挥则去

    适用于:.net2.0+ Winform项目 背景: 有时候我们需要开一个简单的窗口来做一些事,例如输入一些东西.点选一个item之类的,可能像这样: 完了返回原窗体并获取刚刚的输入,这样做并没有什么 ...

  9. sass 变量

    1.使用变量 $符号标识变量 变量名中  中划线和下划线互通(不包括sass中纯 css 部分) 变量值 css 属性标准值 包括以空格 和 逗号 , 分开的多个属性值 变量可以定义在规则块之外

  10. 插入排序实现&&选择排序实现

    萌新刚刚开始学习算法,第一步是学习排序,毕竟算法的四大块"排序,查找,图,字符串"里面,排序是第一位的(PS:今天才知道算法提供的只是一个程序编写思路,一直以为是一个函数,难怪传入 ...