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

相关内容

热门资讯

推荐透视!xpoker辅助!其... 推荐透视!xpoker辅助!其实有辅助软件(有挂详细)-哔哩哔哩1、操作简单,无需xpoker辅助手...
关于透视!xpoker辅助神器... 关于透视!xpoker辅助神器!总是是真的有辅助攻略(有挂技巧)-哔哩哔哩1)xpoker辅助神器免...
分享透视!cloudpoker... 分享透视!cloudpoker作必弊!确实有辅助脚本(有挂秘笈)-哔哩哔哩1、许多玩家不知道clou...
揭露透视!竞技联盟透视!切实是... 揭露透视!竞技联盟透视!切实是真的有辅助软件(有挂技巧)-哔哩哔哩1、该软件可以轻松地帮助玩家将竞技...
关于透视!红龙poker有辅助... 关于透视!红龙poker有辅助吗!切实一直都是有辅助攻略(有挂秘籍)-哔哩哔哩1、下载好红龙poke...
详情透视!智星菠萝透视!本来一... 详情透视!智星菠萝透视!本来一直都是有辅助app(今日头条)-哔哩哔哩1、下载好智星菠萝透视脚本下载...
科普透视!pokernow辅助... 科普透视!pokernow辅助工具!原来存在有辅助方法(有挂工具)-哔哩哔哩1、很好的工具软件,可以...
曝光透视!pokemmo免费脚... 曝光透视!pokemmo免费脚本!都是是有辅助app(有挂方式)-哔哩哔哩1、pokemmo免费脚本...
了解透视!werplan透视挂... 了解透视!werplan透视挂!好像有辅助神器(有挂神器)-哔哩哔哩1、这是跨平台的werplan透...
专业透视!pokemmo修改器... 专业透视!pokemmo修改器手机版!真是是有辅助插件(真是有挂)-哔哩哔哩1、下载好pokemmo...