Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular.js 2:指令的访问组件_Javascript_Angular - Fatal编程技术网

Javascript Angular.js 2:指令的访问组件

Javascript Angular.js 2:指令的访问组件,javascript,angular,Javascript,Angular,考虑一下父级模板的以下片段: <div *ngFor= "let event of events" > <event-thumbnail [theEvent] = 'event'></event-thumbnail> </div> 在Parentcomponent类中,我想迭代所有生成的事件缩略图元素,访问每个元素下的组件,并对其中一个元素调用introductionyourself函数。您的子组

考虑一下
父级
模板的以下片段:

<div *ngFor= "let event of events" >
     <event-thumbnail [theEvent] = 'event'></event-thumbnail>                    
</div>

Parent
component类中,我想迭代所有生成的
事件缩略图
元素,访问每个元素下的组件,并对其中一个元素调用
introductionyourself
函数。

您的子组件应该具有
@Input()事件
,以访问您正在传递的事件。然后,您可以使用以下生命周期挂钩:

ngOnInit(){
   introduceYourself(){
      console.log('I am X');
   }
}

您的子组件应该具有
@Input()事件
,才能访问您正在传递的事件。然后,您可以使用以下生命周期挂钩:

ngOnInit(){
   introduceYourself(){
      console.log('I am X');
   }
}

您希望使用
@ViewChildren()
装饰器获取视图中特定组件类型的所有实例的列表:

class ParentComponent implements AfterViewInit {
    @ViewChildren(EventThumbnailComponent)
    eventThumbnails: QueryList<EventThumbnailComponent>;

    ngAfterViewInit(): void {
        // Loop over your components and call the method on each one
        this.eventThumbnails.forEach(component => component.introduceYourself());

        // You can also subscribe to changes...
        this.eventThumbnails.changes.subscribe(r => {             
            // Do something when the QueryList changes
        });
    }
}
类ParentComponent实现AfterViewInit{
@ViewChildren(EventThumbnailComponent)
事件缩略图:QueryList;
ngAfterViewInit():void{
//在组件上循环并对每个组件调用方法
this.eventThumbnails.forEach(component=>component.introductionYourself());
//您还可以订阅更改。。。
this.eventThumbnails.changes.subscribe(r=>{
//在QueryList更改时执行某些操作
});
}
}
每当在视图中添加或删除此组件的实例时,
eventThumbnails
属性都将更新。请注意,
eventThumbnails
直到
ngAfterViewInit
才设置

有关更多信息,请参阅此处的文档:

您想使用
@ViewChildren()
装饰器获取视图中特定组件类型的所有实例的列表:

class ParentComponent implements AfterViewInit {
    @ViewChildren(EventThumbnailComponent)
    eventThumbnails: QueryList<EventThumbnailComponent>;

    ngAfterViewInit(): void {
        // Loop over your components and call the method on each one
        this.eventThumbnails.forEach(component => component.introduceYourself());

        // You can also subscribe to changes...
        this.eventThumbnails.changes.subscribe(r => {             
            // Do something when the QueryList changes
        });
    }
}
类ParentComponent实现AfterViewInit{
@ViewChildren(EventThumbnailComponent)
事件缩略图:QueryList;
ngAfterViewInit():void{
//在组件上循环并对每个组件调用方法
this.eventThumbnails.forEach(component=>component.introductionYourself());
//您还可以订阅更改。。。
this.eventThumbnails.changes.subscribe(r=>{
//在QueryList更改时执行某些操作
});
}
}
每当在视图中添加或删除此组件的实例时,
eventThumbnails
属性都将更新。请注意,
eventThumbnails
直到
ngAfterViewInit
才设置

有关更多信息,请参阅此处的文档:

是否可能重复?i、 e.允许您的
EventThumbnailComponent
侦听父事件,这样您就不必手动迭代dom元素。特别是,请查看重复的目标。是否可能重复?i、 e.允许您的
EventThumbnailComponent
侦听父事件,这样您就不必手动迭代dom元素。特别是,查看重复的目标。工作起来很有魅力!完美答案。所以没有什么对你不利的,@flovy,但是我们已经倒退到远低于jQuery提供的dom操作抽象级别,这难道不令人伤心吗?工作起来很有魅力!完美答案。因此,没有什么对您不利的,@driffy,但我们已经倒退到远低于jQuery提供的dom操作抽象级别,这难道不令人伤心吗。