当前位置: 首页 > 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
  • #Java异常处理
  • .pyc 想到的一些问题
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • [译]CSS 居中(Center)方法大合集
  • Apache的基本使用
  • AWS实战 - 利用IAM对S3做访问控制
  • ES2017异步函数现已正式可用
  • FineReport中如何实现自动滚屏效果
  • JAVA_NIO系列——Channel和Buffer详解
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • node和express搭建代理服务器(源码)
  • Rancher-k8s加速安装文档
  • Service Worker
  • Vim 折腾记
  • 后端_ThinkPHP5
  • 技术:超级实用的电脑小技巧
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 前端学习笔记之观察者模式
  • 删除表内多余的重复数据
  • 使用Swoole加速Laravel(正式环境中)
  • 算法-插入排序
  • 我是如何设计 Upload 上传组件的
  • 携程小程序初体验
  • ionic异常记录
  • zabbix3.2监控linux磁盘IO
  • ​520就是要宠粉,你的心头书我买单
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • (¥1011)-(一千零一拾一元整)输出
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • .Mobi域名介绍
  • .NET 5种线程安全集合
  • .net 无限分类
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @EventListener注解使用说明