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

相关内容

热门资讯

外挂模板!uupoker有透视... 外挂模板!uupoker有透视吗,约局吧开挂神器是真的吗-一直真的是有辅助方法(哔哩哔哩)1、约局吧...
外挂教材!佛手在线大菠萝辅助,... 外挂教材!佛手在线大菠萝辅助,拱趴大菠萝万能辅助器-都是是真的有辅助攻略(哔哩哔哩)外挂教材!佛手在...
外挂演示!德州私人局脚本,po... 外挂演示!德州私人局脚本,pokermaster修改器-竟然存在有辅助攻略(哔哩哔哩)德州私人局脚本...
外挂项目!xpoker辅助控制... 外挂项目!xpoker辅助控制,pokernow辅助控制-本来真的是有辅助方法(哔哩哔哩)所有人都在...
外挂办法!哈糖大菠萝开挂,德普... 外挂办法!哈糖大菠萝开挂,德普之星私人局辅助免费-原来一直总是有辅助教程(哔哩哔哩)哈糖大菠萝开挂是...
外挂窍门!拱趴大菠萝怎么开挂,... 外挂窍门!拱趴大菠萝怎么开挂,智星菠萝透视-确实有辅助技巧(哔哩哔哩)1、拱趴大菠萝怎么开挂脚本辅助...
外挂课程!菠萝辅助器免费版的功... 外挂课程!菠萝辅助器免费版的功能介绍,来玩app破解-总是真的有辅助神器(哔哩哔哩);进入游戏-大厅...
外挂大纲!约局吧游戏挂,soh... 外挂大纲!约局吧游戏挂,sohoo竞技联盟辅助器-一直真的有辅助app(哔哩哔哩)1)sohoo竞技...
外挂方案!佛手在线有挂吗,so... 外挂方案!佛手在线有挂吗,sohoo辅助-一直一直都是有辅助app(哔哩哔哩)1)佛手在线有挂吗有没...
外挂窍要!pokerworld... 外挂窍要!pokerworld辅助器,约局吧游戏挂-其实有辅助教程(哔哩哔哩)1、pokerworl...