Angular属性指令如何获取其他兄弟属性指令的引用
创始人
2024-10-29 15:31:07
0

在Angular中,可以使用@ContentChildren装饰器来获取其他兄弟属性指令的引用。@ContentChildren装饰器可以用来获取在子组件中使用ng-content插入的内容。

下面是一个示例代码,演示了如何使用@ContentChildren装饰器获取其他兄弟属性指令的引用:

  1. 首先,创建一个父组件,包含两个子组件。
import { Component, ContentChildren, QueryList, AfterContentInit } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-parent',
  template: `
    
    
  `
})
export class ParentComponent implements AfterContentInit {
  @ContentChildren(ChildComponent) children: QueryList;

  ngAfterContentInit() {
    console.log(this.children);
  }
}
  1. 创建一个子组件,并在其中添加一个属性指令。
import { Component, Directive } from '@angular/core';

@Directive({
  selector: '[appAttributeDirective]'
})
export class AttributeDirective {}

@Component({
  selector: 'app-child',
  template: `
    
` }) export class ChildComponent {}

在上面的代码中,@ContentChildren(ChildComponent)装饰器用于获取ChildComponent的实例。然后,我们可以在ngAfterContentInit生命周期钩子函数中访问这个QueryList,并使用它来操作兄弟组件的属性指令。

注意:要使用@ContentChildren装饰器,需要确保在父组件的模板中使用ng-content插入了子组件。

希望这个示例代码能帮助到你!

相关内容

热门资讯

记者获悉!hhpoker透视脚... 记者获悉!hhpoker透视脚本视频(透视)爱玩联盟辅助软件(都是有辅助app)-哔哩哔哩一、爱玩联...
日前!wejoker辅助软件视... 日前!wejoker辅助软件视频(透视)微乐贵阳麻将辅助(一直有辅助下载)-哔哩哔哩1、打开软件启动...
据监测!we-poker靠谱吗... 据监测!we-poker靠谱吗(透视)威信茶馆大贰破解(本来是有辅助app)-哔哩哔哩1、每一步都需...
做出回应!hhpoker哪个俱... 做出回应!hhpoker哪个俱乐部靠谱(透视)捉住捣蛋鸡作必弊(真是存在有辅助插件)-哔哩哔哩1、捉...
透视游戏!德州透视竞技联盟(透... 透视游戏!德州透视竞技联盟(透视)哈灵脚本辅助(本来真的是有辅助平台)-哔哩哔哩哈灵脚本辅助破解侠是...
现有说明如下!aapoker真... 您好,福建天天开心辅助器是真的码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
来临!wepokerplus透... 来临!wepokerplus透视脚本免费(透视)广东雀神祈福辅助器(本来是真的辅助安装)-哔哩哔哩暗...
透视有挂!hhpoker是真的... 透视有挂!hhpoker是真的还是假的(透视)广西老友玩有破解(一直真的有辅助平台)-哔哩哔哩1、上...
推出新举措!we poker游... 推出新举措!we poker游戏下(透视)微信小程序开心泉州辅助器(好像是有辅助工具)-哔哩哔哩1、...
透视实锤!hhpoker到底可... 透视实锤!hhpoker到底可以作必弊吗(透视)天天卡五星辅助(真是有辅助修改器)-哔哩哔哩;1、用...