Angular21 动态绑定CSS样式
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样式的更多相关文章
- Vue之动态绑定CSS样式
demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- Vue 动态绑定CSS样式
今天在做项目上遇见了一个需求,通过不能的进度类型展示不同的进度形态,进度形态通过背景色和背景色上的文字显示. 效果图: 由于Element UI版本我用的是2.5.4 使用进度条的话 就没有2.9. ...
- Vue 框架-05-动态绑定 css 样式
Vue 框架-05-动态绑定 css 样式 今天的小实例是关于 Vue 框架动态绑定 css 样式,这也是非常常用的一个部分 首先说一下 动态绑定,相对的大家都知道静态绑定,静态绑定的话,直接加 cl ...
- css样式让input垂直居中
css样式让input垂直居中 css代码: .div1{ border: 1px solid #CCC; width:1120px; height:40px; margin:auto; displa ...
- 深度理解CSS样式表,内有彩蛋....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js设置css样式.
在js设置css样式做法 var obj = document.getElementById('div'); obj.style.width = '100px'; obj.style.height = ...
- CSS样式表
CSS样式及属性 样式标的基本概念 样式表的分类 1.内联样式表 和html联合显示,控制精确,但可重用性差,冗余多. 例:<p style="font-size:14px;" ...
- 脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)
问题描述: 由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用.但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生 ...
- jQuery所支持的css样式
jQuery所支持的css样式 backgroundPosition borderWidth borderBottomWidth borderLeftWidth borderRightWidth bo ...
随机推荐
- Java基础之Throwable,文件加载
Java中的异常与错误都继承自Throwable,Exception又分为运行时异常(RuntimeException)和编译时异常. 运行时异常是程序的逻辑不够严谨或者特定条件下程序出现了错误,例如 ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- markdown的流程图、时序图、甘特图画法
https://www.jianshu.com/p/a9ff5a9cdb25 Markdown里面的序列图 https://shd101wyy.github.io/markdown-preview-e ...
- Filezilla Server日志文件
Filezilla Server版本:0.9.41. Filezilla Server日志文件在软件安装目录下Logs目录下. 但Filezilla Server默认不开始日志记录,如何开启filez ...
- 【JavaWeb】权限管理系统
前言 前面我们做的小项目都是一个表的,业务代码也相对简单.现在我们来做一个权限管理系统,体验一下多表的业务逻辑,顺便巩固一下过滤器的知识.! 目的 现在我有一个管理商品.订单的页面.当用户点击某个超链 ...
- return的新思考
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- Apache和Tomcat的区别与联系
作者:郭无心链接:https://www.zhihu.com/question/37155807/answer/72706896来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Windows下nexus-3.*搭建Maven私服
1.下载 从官网下载https://help.sonatype.com/display/NXRM3/Download 选择Windows archive https://download.sonaty ...
- 彻底解决Yii2中网页刷新时验证码不刷新的问题
修改vendor/yiisoft/yii2/captcha/CaptchaValidator.php这个文件就可以了,修改的地方见下图: 总结 归根到底,是因为yii2在渲染网页的时候,会先输出js验 ...
- handler机制面试
handler面试必问 解析: Android提供了Handler和Looper来满足线程间的通信. Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(Message E ...