简体   繁体   English

如何在angular中找到class名字的长度 12

[英]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.

相关问题 导入自定义 Angular 库时,Angular 12 和 Webpack 5“找不到导出的节点名称” - Angular 12 & Webpack 5 "Failed to find exported name of node" when importing custom Angular library Angular 12+ 如何使用名称(字符串)或选择器(字符串)加载组件 - Angular 12+ How to load component with Name (string) or selector (string) 在 angular 12 中选中复选框时,如何在父级 (tr) 中切换 class - How to toggle class in parent (tr) when checkbox is checked in angular 12 如何从Angular中的类名中找到组件类的原型并获取在该类原型中定义的属性的值? - How can I find the prototype of component class from class name in Angular and get the value of property defined in that class prototype? Angular中组件名称的最大长度? - Maximum length of a component name in Angular? 如何给角2/4中的动画状态赋予类名? - How to give class name to animation state in angular 2/4? 如何在 angular 中交替更改 class 名称 - How to change class name alternatively in angular 如何在Angular 7 / Typescript中使用变量类名? - How to use variable class name in Angular 7 / Typescript? 如何在角度2中动态更改css类名称 - How to change the css class name dynamically in angular 2 如何将类名称添加到Angular2指令 - How to add Class name to Angular2 directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM