jquery - 角度自动目录
问题描述
我正在尝试以角度制作自动目录。为此,我需要通过标签获取元素(h1,h2..etc)。有谁知道 Angular 中 jquery 的等效功能document.getelementbytagname ('h1, h2, h3')
?ViewChildren 不起作用,因为它无法按标签查找项目。ViewChildren 与#id 和组件一起使用。我需要帮助!我提前感谢
解决方案
如果我理解正确,那么这将解决您的问题
import { Component, Input, ElementRef, Inject, OnInit} from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'hello',
template: `<h1>Hello {{name}}!</h1><h1>Hello {{name}}!</h1><h1>Hello {{name}}!</h1>`,
styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent implements OnInit {
@Input() name: string;
constructor(@Inject(DOCUMENT) private document: HTMLElement){
}
ngOnInit() {
console.log(this.document.getElementsByTagName('h1'));
}
}
推荐阅读
- c - 如何在 C 中返回 long 的结束数字(特别是当它们为 0 时)
- javascript - 在使用 Javascript 或 HTML 提交之前修改输入值
- angular - 无法在模态中动态加载或延迟加载组件
- ruby - Browsermob Proxy + Watir 没有连续捕获流量
- javascript - Express:为定义的路由提供静态文件
- php - 在文本框中从数据库中搜索数据以显示 - Laravel
- py2app - 如何引用通过 data_files 属性添加的文件?
- oracle - 如何在plsql中获取当前日期时间?
- c# - Microsoft.Crm.Sdk.Samples.HelperCode 无法编译
- javascript - 谷歌地图如何在 map-active.js 中添加第二个位置