angular6 - 我可以在 ViewChildren 中使用 html 标记作为选择器吗
问题描述
我想让<p>
模板中的所有内容都具有font-variant
small-caps
. 我可以通过html
toViewChildren
吗?我见过传递模板引用变量或组件的示例,但还没有看到传递给的选择器ViewChildren
是html
标签或css
类的示例。
template: `<p>
I am a static component
</p>
<p>
I am another para but I have an id (template reference variable) #pref. I got my blue color using ViewChild and Renderer2
</p>
<p>
All paragraphs are small-caps because using ViewChildren and Renderer2, the style was set to bold
</p>`
.ts 文件
import { Component,ViewChild, ViewChildren, QueryList,ElementRef,Renderer2,OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
@ViewChildren("p") paraList:QueryList<any>; /*I SUPPOSE I CANT DO THIS AS P IS INTERPRETED AS TEMPLATE REFERENCE VARIABLE AND NOT <P>*/
constructor(private renderer:Renderer2){
}
ngAfterViewInit(){ //QueryList becomes available now
this.paraList.forEach(para =>{this.renderer.addClass(para,'boldClass')})
}
}
css
.boldClass{
font-variant: small-caps;
}
解决方案
推荐阅读
- python - 如何在 RandomForestClassifier 中选择 n_estimators?
- java - sudo ln -sfn 有什么作用?
- gremlin - Gremlin 查询不起作用 - 如果不存在,则为 2 x 顶点并添加边
- javascript - 如何使用 javascript 将数据上传到电子邮件
- javascript - 绑定数据时出现Angular Elements Web Component错误
- sql - 在 C 中编写 PostgreSQL 函数:经典 SPI_execq 示例 - 不打印为日志消息?
- java - 尝试使用 int Java 反转 123 _> 321
- arrays - fortran中不兼容的等级0和1
- windows - 与 Windows server 2019 中的 Vram 相关的问题
- python - Python settrace 仅显示根调用而不显示后续调用