[英]How to find the length of class name in angular 12
Trying to get the length of dropdown class name which one have 'vehicle-disabled' class name.试图获取下拉列表 class 名称的长度,其中一个具有“车辆禁用”class 名称。
I have tried but not working i do not know how to calculate particular length of class name(.vehicle-disabled ).我已经尝试但没有工作我不知道如何计算 class 名称(.vehicle-disabled)的特定长度。 If anyone knows please help to find the solution.
如果有人知道请帮助找到解决方案。
app.component.html:应用程序组件.html:
<div #gdropdown>
<mag-dropdown>
<div id="general-dp" class="vehicle-disabled"></div>
</mag-dropdown>
<mag-dropdown>
<div id="general-dp" class=""></div>
</mag-dropdown>
<mag-dropdown>
<div id="general-dp" class="vehicle-disabled"></div>
</mag-dropdown>
</div>
app.component.ts:应用程序组件.ts:
export class AppComponent implements OnInit {
name = 'Angular ' + VERSION.major;
@ViewChild('gdropdown') finddisabled: ElementRef<HTMLElement>;
ngOnInit(){
let arr= [];
const dom: HTMLElement = this.finddisabled.nativeElement;
const elements = dom.querySelectorAll('.vehicle-disabled');
this.arr.push(elements);
console.log(arr.length);
}
}
Demo: https://stackblitz.com/edit/angular-ivy-5kc12t?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts演示: https://stackblitz.com/edit/angular-ivy-5kc12t?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts
This answer is based on the expectation, that you mean amount of elements with this classname.这个答案是基于预期,你的意思是这个类名的元素数量。 (The length of 'vehicle-disabled' is obviously 16, since this classname has 16 characters)
('vehicle-disabled' 的长度显然是 16,因为这个类名有 16 个字符)
Please use AfterViewInit instead of OnInit and use the actual array length请使用 AfterViewInit 而不是 OnInit 并使用实际的数组长度
ngAfterViewInit() {
const dom: HTMLElement = this.finddisabled.nativeElement;
const elements = dom.querySelectorAll('.vehicle-disabled');
console.log(elements.length);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.