当前位置: 首页 > news >正文

[Angular] 笔记 6:ngStyle

ngStyle 指令:

用于更新 HTML 元素的样式。设置一个或多个样式属性,用以冒号分隔的键值对指定。键是样式名称,带有可选的 .<unit> 后缀(如 ‘top.px’、‘font-style.em’),值为待求值的表达式,得到的非空值以给定的单位表示,并赋给指定的样式属性。如果求值结果为 null,则相应的样式将被移除。

( An attribute directive that updates styles for the containing HTML element. Sets one or more style properties, specified as colon-separated key-value pairs. The key is a style name, with an optional . suffix (such as ‘top.px’, ‘font-style.em’). The value is an expression to be evaluated. The resulting non-null value, expressed in the given unit, is assigned to the given style property. If the result of evaluation is null, the corresponding style is removed. )


chatgpt 回答:
在 Angular 中,ngStyle 是一个内置的指令,用于动态设置 HTML 元素的样式。它允许你根据组件中的条件或变量值动态地修改元素的样式。ngStyle 指令接受一个对象作为输入,其中键是 CSS 样式属性,值是对应的样式值。这使得你可以根据组件中的变量值来动态地添加或移除样式。

例如,你可以这样使用 ngStyle 指令:


<div [ngStyle]="{'color': textColor, 'font-size.px': fontSize, 'font-style': isItalic ? 'italic' : 'normal'}">This text has dynamic styles applied.
</div>

在这个例子中,textColorfontSizeisItalic 是组件中的变量,它们根据不同的值来动态地修改文本的颜色、字体大小和字体样式。


Stackoverflow 例子:

<div [style.background-color]="style1 ? 'red' : (style2 ? 'blue' : null)">

等价于:

<div [ngStyle]="{'background-color': style1 ? 'red' : (style2 ? 'blue' : null) }">

Youtube tutorial:

ngStyle 作用与 ngClass 类似,用于设置 HTML 元素样式,但不是使用自定义 css文件或者bootstrap,而是直接在行内引入样式:
在这里插入图片描述

app.component.ts:

import { Component } from '@angular/core';// 使用类型检查
interface Pokemon {id: number;name: string;type: string;isCool: boolean;isStylish: boolean;
}@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css'],
})
export class AppComponent {pokemons: Pokemon[] = [{id: 1,name: 'pikachu',type: 'electric',isCool: false,isStylish: true,},{id: 2,name: 'squirtle',type: 'water',isCool: true,isStylish: true,},{id: 3,name: 'charmander',type: 'fire',isCool: true,isStylish: false,},];constructor() {}
}

app.component.html,其中 ngClassngStyle 两种指令都有使用:

<table><thead><th>Name</th><th>Index</th></thead><tbody><tr *ngFor="let pokemon of pokemons; let i = index"><td class="pokemon-td" [class.cool-bool]="pokemon.isCool">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><td class="pokemon-td" [ngClass]="{ 'cool-bool': pokemon.isCool }">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><tdclass="pokemon-td"[style.backgroundColor]="pokemon.isStylish ? '#800080' : ''">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><tdclass="pokemon-td"[ngStyle]="{ 'backgroundColor': (pokemon.isStylish ? '#800080' : '') }">{{ i }} {{ pokemon.name }}</td></tr></tbody>
</table>

Web 页面:

在这里插入图片描述

相关文章:

  • cesium实现区域贴图及加载多个gif动图
  • 基于AR+地图导航的景区智慧导览设计
  • 新版IDEA中Git的使用(一)
  • 1.Linux是什么与如何学习
  • 智能优化算法应用:基于人工兔算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • 【三维目标检测】【自动驾驶】IA-BEV:基于结构先验和自增强学习的实例感知三维目标检测(AAAI 2024)
  • R语言使用scitb包10分钟快速绘制论文基线表
  • AndroidStudio无法新建aidl文件解决办法
  • 单例模式(C++实现)
  • C/C++常见面试题(四)
  • C/C++图形化编程(1)
  • Spring Boot国际化i18n配置指南
  • 华清远见嵌入式学习——ARM——作业3
  • 16.Redis 高级数据类型 + 网站数据统计
  • 关于“Python”的核心知识点整理大全34
  • 分享的文章《人生如棋》
  • 30天自制操作系统-2
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Bootstrap JS插件Alert源码分析
  • Iterator 和 for...of 循环
  • JavaScript HTML DOM
  • javascript从右向左截取指定位数字符的3种方法
  • maven工程打包jar以及java jar命令的classpath使用
  • Vue小说阅读器(仿追书神器)
  • 阿里云购买磁盘后挂载
  • 当SetTimeout遇到了字符串
  • 汉诺塔算法
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 入口文件开始,分析Vue源码实现
  • 深入浏览器事件循环的本质
  • 微信小程序设置上一页数据
  • 你对linux中grep命令知道多少?
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • #include
  • #大学#套接字
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • (13):Silverlight 2 数据与通信之WebRequest
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (待修改)PyG安装步骤
  • (二)pulsar安装在独立的docker中,python测试
  • (二)学习JVM —— 垃圾回收机制
  • (一)为什么要选择C++
  • (原創) 物件導向與老子思想 (OO)
  • (转)ORM
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转)创业的注意事项
  • (转)机器学习的数学基础(1)--Dirichlet分布