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引用,以确保子组件或嵌套组件已经初始化完毕。

相关内容

热门资讯

来临!wepoker怎么挂飞机... 来临!wepoker怎么挂飞机,边锋干瞪眼房租胡,竟然是真的有辅助脚本(哔哩哔哩)在进入边锋干瞪眼房...
曝光透视!哈糖大菠萝万能挂,佛... 曝光透视!哈糖大菠萝万能挂,佛手在线是不是有挂(透视)一直真的有辅助技巧(哔哩哔哩)1)佛手在线是不...
据公告内容!来玩app破解,宝... 据公告内容!来玩app破解,宝宝浙江辅助工具,好像真的是有辅助脚本(哔哩哔哩)1、进入到宝宝浙江辅助...
有挂透视!xpoker辅助控制... 有挂透视!xpoker辅助控制,we poker免费辅助器(透视)果然一直都是有辅助攻略(哔哩哔哩)...
现有关情况通报如下!aapok... 您好,新众亿卡看底牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
解迷透视!哈糖大菠萝有没有挂,... 解迷透视!哈糖大菠萝有没有挂,约局吧德州可以透视吗(透视)真是有辅助脚本(哔哩哔哩)1、约局吧德州可...
相较于以往!wpk俱乐部是做什... 相较于以往!wpk俱乐部是做什么的,赣牌圈总输,确实真的有辅助脚本(哔哩哔哩)1)赣牌圈总输有没有挂...
了解透视!约局吧app有挂吗,... 了解透视!约局吧app有挂吗,xpoker辅助工具(透视)好像有辅助技巧(哔哩哔哩)1、约局吧app...
刚刚!wepoker公共底牌,... 刚刚!wepoker公共底牌,乐酷有没有挂,其实是真的有辅助神器(哔哩哔哩)乐酷有没有挂是不是有人用...
揭幕透视!pokemmo辅助器... 揭幕透视!pokemmo辅助器脚本下载,菠萝德州透视脚本(透视)原来真的有辅助教程(哔哩哔哩)1、任...