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

相关内容

热门资讯

普及透视!拱趴大菠萝机器人,p... 普及透视!拱趴大菠萝机器人,poker world辅助(透视)切实一直总是有辅助神器(哔哩哔哩)1、...
辅助透视!红龙poker辅助工... 辅助透视!红龙poker辅助工具,pokemmo修改器手机版(透视)都是一直总是有辅助方法(哔哩哔哩...
教你透视!哈糖大菠萝可以开挂吗... 教你透视!哈糖大菠萝可以开挂吗,拱趴大菠萝作必弊方法(透视)原来真的是有辅助教程(哔哩哔哩)1、拱趴...
揭幕透视!poker worl... 揭幕透视!poker world辅助器,智星德州可以透视吗(透视)总是有辅助app(哔哩哔哩)1、每...
曝光透视!佛手在线有挂吗,xp... 曝光透视!佛手在线有挂吗,xpoker辅助助手(透视)好像真的是有辅助神器(哔哩哔哩)小薇(辅助器软...
解谜透视!拱趴大菠萝自动计算机... 解谜透视!拱趴大菠萝自动计算机器人,pokemmo手机版修改器(透视)其实是真的有辅助工具(哔哩哔哩...
揭幕透视!拱趴大菠萝有挂吗,p... 揭幕透视!拱趴大菠萝有挂吗,pokermaster脚本(透视)都是是真的有辅助神器(哔哩哔哩)1、操...
了解透视!拱趴大菠萝有挂吗,拱... 了解透视!拱趴大菠萝有挂吗,拱趴大菠萝作必弊方法(透视)原来一直总是有辅助工具(哔哩哔哩)拱趴大菠萝...
揭露透视!来玩app破解版,福... 揭露透视!来玩app破解版,福建大菠萝万能辅助器(透视)切实真的有辅助技巧(哔哩哔哩)1、首先打开福...
曝光透视!uupoker有透视... 曝光透视!uupoker有透视吗,aa poker辅助包(透视)一直有辅助脚本(哔哩哔哩)1、uup...