使用指令将参数传递给子组件angular

汉斯

我试图创建可以使用指令传递参数的子组件,但我仍然找不到任何创建itu的教程。

我从中尝试了一些方法:获取对组件中使用的指令的引用

这是我的代码

这是父组件的模板

<child-component>
    <grand-child-directive [name]='Ghandy'></grand-child-directive >
    <grand-child-directive [name]='Ani'></grand-child-directive >
    <grand-child-directive [name]='Budi'></grand-child-directive >
</child-component>

这个孙子指令

@Directive({
  selector: 'grand-child-directive ',
})
export class gc{
  @Input() name:string;
}

这是我的孩子

@Component({
  selector: 'child-component',
  templateUrl: './child-component.component.html',
  styleUrls: ['./child-component.component.scss'],

})

export class childComponent implements OnInit 

@ViewChildren(gc) gc: gc;

  constructor(
  ) {

  }
  ngOnInit() {
    console.log(gc.name)
  }
}

当我console.log gc.name时,我不确定,而不是数组[Ghandy,Ani,Budi]

我很乐意提供任何帮助。

Chellappan w

您应该使用ContentChildren而不是ViewChildren。然后,您应该实现AfterContentInit生命周期挂钩以访问该值。

@Component({
  selector: 'child-component',
  templateUrl: './child-component.component.html',
  styleUrls: ['./child-component.component.scss'],

})

export class childComponent implements OnInit,AfterContentInit 

 @ContentChildren(GrandChildDirective) gc:QueryList<GrandChildDirective> ;

  constructor(
  ) {

  }
  ngAfterContentInit() {
     console.log(this.gc.toArray().map(item=>item.name));
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用模板参数将函数传递给Angular指令

来自分类Dev

Angular js将变量从父指令传递给子指令

来自分类Dev

将路径参数传递给子组件作为Angular 2中的输入

来自分类Dev

将路径参数传递给子组件作为Angular 2中的输入

来自分类Dev

将值传递给子组件并将其用作 Angular 中的查询参数

来自分类Dev

如何将参数传递给 angular 中的指令函数?

来自分类Dev

如何使用ngFor指令将子ID传递给Angular 2中的父对象

来自分类Dev

将函数传递给指令,然后使用参数调用它

来自分类Dev

将值传递给子组件Angular2

来自分类Dev

将集合传递给 Angular 中的子组件

来自分类Dev

Angular 将 API 响应传递给子组件

来自分类Dev

将参数传递给Angular组件中的getter的替代方法

来自分类Dev

Angular 2将字符串参数传递给组件

来自分类Dev

Angular 7:将参数传递给其他组件错误

来自分类Dev

angularjs:将参数传递给动态指令

来自分类Dev

使用@Input装饰器将函数传递给子组件

来自分类Dev

如何在不使用angular的情况下将输入值从父组件传递给子组件?

来自分类Dev

angular templateRef 使用结构指令传递给父组件而不在父组件内呈现

来自分类Dev

将 Angular 变量传递给指令

来自分类Dev

将值从父指令模板传递给子指令

来自分类Dev

将变量从父指令传递给子指令?

来自分类Dev

如何将指令参数传递给指令

来自分类Dev

React:将功能传递给子组件

来自分类Dev

将道具传递给子组件的问题

来自分类Dev

将React Route传递给子组件

来自分类Dev

将模型传递给子组件

来自分类Dev

将多个引用传递给子组件

来自分类Dev

将事件结果传递给子组件

来自分类Dev

将方法传递给子组件

Related 相关文章

热门标签

归档