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钻石怎么看底牌(透视)确实真的有挂(详细辅助必赢方法);《WPK辅助透视》‌...
透视私人局!aapoker辅助... 透视私人局!aapoker辅助是真的吗,hhpoker作弊码怎么用(详细辅助靠谱教程)1、点击下载安...
透视透视!wepoker透视,... 透视透视!wepoker透视,wepoker透视挂底牌(详细辅助切实教程)1)wepoker透视辅助...
透视教学!wpk透视是真的吗(... 您好,wpk透视这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游...
透视挂透视!拱趴大菠萝机器人(... 《透视挂透视!拱趴大菠萝机器人(透视)果然存在有挂(详细辅助2025新版总结)》 软件透明挂更新公告...
透视科技!aapoker脚本怎... 透视科技!aapoker脚本怎么用,德普之星透视辅助插件(详细辅助解密教程)1)德普之星透视辅助插件...
透视脚本!wpk透视辅助软件,... 透视脚本!wpk透视辅助软件,epoker透视(详细辅助必胜教程);wpk透视辅助软件是一种具有地方...
透视规律!wepoker私人局... 透视规律!wepoker私人局透视插件(透视)起初真的是有挂(详细辅助wpk教程);小薇(透视辅助)...
透视透视!wepoker底牌透... 透视透视!wepoker底牌透视脚本app,菠萝德州透视脚本(详细辅助可靠教程)1、上手简单,内置详...