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

相关内容

热门资讯

第6分钟下载!微乐小程序免费黑... 第6分钟下载!微乐小程序免费黑科技,微信微乐小程序辅助器免费安装(开挂)其实是真的挂(2026)1、...
无独有偶!微乐小程序免费黑科技... 无独有偶!微乐小程序免费黑科技,微乐云南小程序修改器ios(透视)确实真的是有挂(哔哩哔哩)1、进入...
四分钟插件!微乐小程序免费黑科... 四分钟插件!微乐小程序免费黑科技,微乐脚本(开挂)总是存在有挂(2026)1、让任何用户在无需微乐小...
此事迅速冲上热搜!微乐小程序免... 此事迅速冲上热搜!微乐小程序免费黑科技,微乐小程序自建房插件怎么安装(透视)一直真的是有挂(哔哩哔哩...
第一分钟教程!微乐小程序黑科技... 第一分钟教程!微乐小程序黑科技,微信小程序微乐房间有挂吗(开挂)好像有挂(2026)1)微乐小程序黑...
透视好牌!微乐小程序免费黑科技... 透视好牌!微乐小程序免费黑科技,微信小程序微乐破解器2024(透视)一直存在有挂(哔哩哔哩)微信小程...
第八分钟下载!微乐小程序黑科技... 第八分钟下载!微乐小程序黑科技,微乐钻石兑换码(开挂)其实存在有挂(2026)微乐小程序黑科技透视方...
透视代打!微乐小程序黑科技,微... 透视代打!微乐小程序黑科技,微乐自建房辅助工具在哪(透视)都是真的是有挂(哔哩哔哩)1.微乐自建房辅...
9分钟神器!微乐小程序黑科技,... 9分钟神器!微乐小程序黑科技,微信微乐游戏辅助脚本(开挂)原来是有挂(2026)1、玩家可以在微乐小...
第三方技巧!微乐小程序黑科技,... 第三方技巧!微乐小程序黑科技,微乐麻辣自建房辅助视频(透视)总是存在有挂(哔哩哔哩)1、玩家可以在微...