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

相关内容

热门资讯

揭幕透视!德普辅助器怎么用,微... 揭幕透视!德普辅助器怎么用,微信微乐游戏辅助脚本,积累教程(有挂秘诀)-哔哩哔哩1、进入到德普辅助器...
科普透视!aapoker免费透... 科普透视!aapoker免费透视脚本,微乐自建房黑科技工具怎么找,方案教程(真的有挂)-哔哩哔哩1、...
解密透视!hhpoker透视工... 解密透视!hhpoker透视工具,微乐小程序辅助收费,项目教程(证实有挂)-哔哩哔哩1、玩家可以在h...
揭幕透视!约局吧作弊脚本,微乐... 揭幕透视!约局吧作弊脚本,微乐自建房免费黑科技下载,资料教程(有挂细节)-哔哩哔哩1、揭幕透视!约局...
推荐透视!hhpkoer辅助挂... 推荐透视!hhpkoer辅助挂是真的吗,微信小程序游戏辅助,攻略教程(真是有挂)-哔哩哔哩运hhpk...
分享透视!wepoker破解器... 分享透视!wepoker破解器,广西微乐小程序脚本,秘籍教程(有挂教程)-哔哩哔哩1、起透看视 we...
关于透视!we poker辅助... 关于透视!we poker辅助器v3.3,小程序微乐游戏辅助器,模板教程(发现有挂)-哔哩哔哩we ...
分享透视!wpk刷入池率脚本,... 分享透视!wpk刷入池率脚本,微信小程序微乐怎么才能发好牌,讲义教程(有挂存在)-哔哩哔哩1、玩家可...
揭幕透视!智星德州可以透视吗,... 揭幕透视!智星德州可以透视吗,微信微乐跑得快游戏辅助脚本,绝活儿教程(有挂教学)-哔哩哔哩1、在智星...
教你透视!hhpoker有后台... 教你透视!hhpoker有后台操控吗,小程序微乐辅助器下载,操作教程(有挂秘诀)-哔哩哔哩1、点击下...