Angular 如何在Typescript中获取角度分量选择器值
我想从Angular 如何在Typescript中获取角度分量选择器值,angular,typescript,Angular,Typescript,我想从ngOnInit方法中获取选择器名称“app navigation”。 我该怎么做 @Component({ selector: 'app-navigation', templateUrl: './navigation.component.html', styleUrls: ['./navigation.component.css'] }) export class Navigation implements OnInit { constructor() {} ngO
ngOnInit
方法中获取选择器
名称“app navigation”。
我该怎么做
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class Navigation implements OnInit {
constructor() {}
ngOnInit() {
console.log("...what is my selector name?")
}
试试这个:
import { Component, ElementRef } from '@angular/core'
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class Navigation implements OnInit {
constructor(elemRef: ElementRef) {
const selectorName = elemRef.nativeElement.tagName.toLowerCase();
}
}
试试这个:
import { Component, ElementRef } from '@angular/core'
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class Navigation implements OnInit {
constructor(elemRef: ElementRef) {
const selectorName = elemRef.nativeElement.tagName.toLowerCase();
}
}
如果选择器包含大写字母,该怎么办?为了获得最佳的角度选择实践,您的自定义组件选择器必须小写。看这里:不是每个人都会遵循最佳实践。答案是,无法获得HTMLDOM树中选择器的实际情况。Accd。返回的标记名始终采用规范的大写形式。如果选择器包含大写字母,该怎么办?要获得使用angular的最佳实践,您的自定义组件选择器必须采用小写形式。看这里:不是每个人都会遵循最佳实践。答案是,无法获得HTMLDOM树中选择器的实际情况。Accd。返回的标记名始终采用规范的大写形式。