[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 页面:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/295687.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从零构建tomcat环境

一、官网构建 1.1 下载 一般来说对于开源软件都有自己的官方网站&#xff0c;并且会附上使用文档以及一些特性和二次构建的方法&#xff0c;那么我们首先的话需要从官网或者tomcat上下载到我们需要的源码包。下载地址&#xff1a;官网、Github。 这里需要声明一下&#xff…

arm和x86架构服务器拉取arm64架构的docker镜像

dockerhub提供的镜像部分支持arm64架构 Docker arm架构服务器拉取docker镜像&#xff0c;默认是arm架构 # docker pull centos Using default tag: latest latest: Pulling from library/centos 52f9ef134af7: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8…

springboot实现发送邮件开箱即用

springboot实现发送邮件开箱即用 环境依赖包yml配置Service层Controller层测试 环境 jdk17 springboot版本3.2.1 依赖包 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><ver…

C++初阶——类和对象

呀哈喽&#xff0c;我是结衣 C入门之后&#xff0c;我们就进入了C的初阶的学习了&#xff0c;在了解类和对象之前&#xff0c;我们还是先了解&#xff0c;面向过程和面向对象的初步认识。 在本篇博客中&#xff0c;我们要讲的内容有 1.面向过程和面向对象初步认识 2.类的引入 3…

DALL-E:Zero-Shot Text-to-Image Generation

DALL-E 论文是一个文本生成图片模型。 训练分为两个阶段 第一阶段&#xff0c;训练一个dVAE&#xff08;discrete variational autoencoder离散变分自动编码器&#xff09;&#xff0c;其将256 x 256的RGB图片转换为32 x 32的图片token。目的&#xff1a;降低图片的分辨率。图…

【ubuntu 22.04】安装中文版系统、中文语言包和中文输入法

在系统安装中的键盘布局选择时&#xff0c;选择Chinese - Chinese&#xff0c;此时会自动安装所有的中文语言包和ibus中文输入法系统安装成功重启后&#xff0c;点击设置 - 区域和语言 - 管理已安装的语言 * 根据提示安装更新后&#xff0c;将汉语&#xff08;中国&#xff09;…

小白入门之安装Navicat

重生之我在大四学JAVA 第四章 安装Navicat (mysql可视化工具) 这里Navicat是15版本&#xff0c;不是最新版&#xff0c;有新版强迫症的自行百度 傻瓜式安装一直下一步就行 完成后切记不要打开&#xff0c;不要打开&#xff0c;不要打开 可以打开刚刚安装的navicat了 切…

arm汇编-补充-画图记忆 LDM/STM系列指令

说明 原始文章系列 https://azeria-labs.com/writing-arm-assembly-part-1/ 翻译-进作者的专栏可以翻到所有翻译 系列&#xff1a;https://zhuanlan.zhihu.com/p/109057983 系列&#xff1a;https://www.anquanke.com/post/id/86383 0x00 调试环境 使用arm架构的系统&#…

python时间处理方法和模块

在 Python 中&#xff0c;有一些内置的模块和库&#xff0c;可以帮助我们处理日期和时间的表示、计算和转换。 1. 时间模块&#xff08;time&#xff09; Python 的 time 模块提供了一系列函数来处理时间相关的操作。通过这个模块&#xff0c;可以获取当前时间、睡眠指定时间…

1161转进制(C语言)

一&#xff1a;题目 二&#xff1a;思路分析 1.首先该题目让我们使用递归求十进制转其他进制 2.其次&#xff0c;我们要知道十进制转换为其他进制怎么转换&#xff0c;以例题所给的数据为例 由此图可以看出&#xff0c;十进制转换为其他进制&#xff0c;是辗转相除法&#xf…

性能实战(一) --- clock_gettime造成系统整体cpu过高定位过程

问题背景 有一台linux服务器测试环境cpu经常到达80%,造成系统卡顿,部分功能不可用. 分析步骤 1.使用perf制作cpu火焰图 通过制作cpu火焰图,发现很多进程都存在大量的clock_gettime系统调用. 2. 使用bcc工具funclatency`进一步查看clock_gettime的调用次数 # /usr/share/bc…

SpringMVC:Ajax、拦截器、文件上传、文件下载

文章目录 SpringMVC - 06一、Ajax1. 概述2. Ajax 异步加载数据1. 单个数据2. 对象 3. 实践4. 总结 二、拦截器1. 概述2. 实现3. 实践4. 总结 三、文件上传&#xff1a;Upload1. 准备工作2. 步骤3. 效果 四、文件下载&#xff1a;Download1. 步骤2. 效果3. 总结 注意&#xff1a…