Angular找到控件名称属于哪个嵌套组
创始人
2024-10-30 22:00:43
0

在Angular中,可以使用@ViewChild装饰器来获取对应的子组件或嵌套组件。以下是一个示例代码,演示如何找到控件名称属于哪个嵌套组:

在父组件中,首先使用@ViewChild装饰器来获取对应的子组件或嵌套组件的引用。然后,可以通过引用的nativeElement属性来访问DOM元素,并使用getAttribute方法获取控件的名称。最后,可以根据控件名称判断它属于哪个嵌套组。

import { Component, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    
` }) export class ParentComponent { @ViewChild(ChildComponent) childComponent: ChildComponent; ngAfterViewInit() { const controlName = this.childComponent.controlName.nativeElement.getAttribute('name'); if (controlName === 'nestedGroup1') { console.log('Control belongs to nested group 1'); } else if (controlName === 'nestedGroup2') { console.log('Control belongs to nested group 2'); } else { console.log('Control does not belong to any nested group'); } } } @Component({ selector: 'app-child', template: `
` }) export class ChildComponent { @ViewChild('controlName') controlName: ElementRef; }

在上面的示例中,父组件ParentComponent使用@ViewChild装饰器获取了ChildComponent的引用。然后,在ngAfterViewInit生命周期钩子中,通过controlName.nativeElement.getAttribute('name')获取控件的名称,并根据名称判断它属于哪个嵌套组。

请注意,必须在ngAfterViewInit生命周期钩子中访问ViewChild引用,以确保子组件或嵌套组件已经初始化完毕。

相关内容

热门资讯

透视游戏!wpk透视脚本下载,... 透视游戏!wpk透视脚本下载,约局吧辅助器,详细教程(固有真的有挂);wpk透视辅助官方版是专为公司...
透视线上!wpk脚本辅助器,h... 【福星临门,好运相随】;透视线上!wpk脚本辅助器,hhpoker哪个俱乐部靠谱,软件教程(往昔是有...
透视总结!hhpoker辅助下... 透视总结!hhpoker辅助下载,wepoker透视app下载,透明挂教程(果然存在有挂);超受欢迎...
透视系统!wpk有没有脚本,德... 透视系统!wpk有没有脚本,德扑圈透视,高科技教程(好像真的有挂);原来确实真的有挂(需添加指定薇1...
透视科技!wepoker辅助器... 透视科技!wepoker辅助器是真的的吗,德州透视脚本,曝光教程(确实真的是有挂);原来确实真的有挂...
透视模拟器!wepoker辅助... 透视模拟器!wepoker辅助透视软件,wepoker黑侠辅助器,德州论坛(从来存在有挂);实战中需...
透视辅助!wepoker透视,... 透视辅助!wepoker透视,德普辅助器辅助器怎么用,软件教程(一直真的有挂);玩家必备必赢加哟《1...
透视模拟器!aapoker脚本... 1、透视模拟器!aapoker脚本怎么用,poker红龙辅助,wpk教程(其实有挂)(UU poke...
透视私人局!wepoker透视... 1、透视私人局!wepoker透视脚本下载,aapoker辅助软件合法吗,力荐教程(原来真的有挂)。...
透视了解!wepoker底牌透... 透视了解!wepoker底牌透视脚本怎样安装,wepoker透视底牌,可靠技巧(一直是有挂)是一款可...