https://stackoverflow.com/questions/50935728/access-viewchild-from-another-component

=================

I have two components, one videoComponent and videoControlsComponent. The video component contains a <video> element and the video component has some buttons to manipulate the videoComponent <video> element.

<video controls="{{ controls }}" [src]="streamUrl" #myVideo>
Your browser does not support the video tag or the file format of this video.
</video>

videoComponent:

@ViewChild('myVideo') myVideo: any;
public playVideo() {
this.myVideo.nativeElement.play();
}

videoControlComponent:

constructor(private videoComponent: VideoComponent) { }
public videoPlay() {
this.videoComponent.playVideo()
}

The problem is that when I click the button I get the following error: Cannot read property 'nativeElement' of undefined at VideoControlsComponent.

But when I have exactly the same code but create the button not in the videoControlsComponent but videoComponent everything works fine.

Can you help me out please?

asked Jun 19 '18 at 19:34
2

you need to use @ViewChild like you did with "myVideo" with videoComponent as well so like this @ViewChild(VideoComponent) videoComponent: VideoComponent

that's assuming videoComponent is a child of videoControls

if they are siblings you can use @Output to trigger an event in the parent, the parent would then change a boolean that is set to an input in videoControls and then set up ngOnChanges on videoControls to detect when that input changes

or you can set up a service to communicate between them. That might be the easiest option if they are not a parent-child relationship

Example of a Service to communicate between components:

@Injectable()
export class MyService {
private myFunctionCallSource = new Subject(); myFunctionCalled$ = this.myFunctionCallSource.asObservable(); callMyFunction(){
this.myFunctionCallSource.next()
}
}

in videoComponent

this.myService.myFunctionCalled$.subscribe(
res => this.myVideo.nativeElement.play(),
err => console.log('MyService error', err)
);

in videoControlsComponent

this.myService.callMyFucnction()

Access viewchild from another component的更多相关文章

  1. [Angular] @ViewChild read custom directive and exportAs

    For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...

  2. [Angular] Difference between ViewChild and ContentChild

    *The children element which are located inside of its template of a component are called *view child ...

  3. Angular 2 中的 ViewChild 和 ViewChildren

    https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...

  4. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  5. 登陆peoplesoft的时候显示信息

    Signon Event Message Select selectPeopleTools, then selectUtilities, then selectAdministration, then ...

  6. eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法

    当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...

  7. hibernate学习(一)配置,导包

    框架的作用 学过javaWeb基础的已经对web层 jsp  servlet   ,service  层  ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...

  8. salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型

    看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...

  9. Ionic2 渐变隐藏导航栏|标题栏

    废话少说 直接上代码.... //导入需要用到的命名空间 ViewChild,Content import { Component, ViewChild } from '@angular/core'; ...

随机推荐

  1. DNS域名解析系统_1

    DNS服务概述: DNS的模式为C/S模式 DNS(Domain Name System)域名系统,在TCP/IP网络中有非常重要的地位,能够提供域名与ip地址的解析服务. DNS是一个分布式数据库, ...

  2. Git速成学习第四课:解决冲突

    Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 我们继续练习,准备新的feature1分支. $ git checkout -b feature ...

  3. Linux 系统信息查询大全

    Linux常用系统命令 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /etc/redhat-release ...

  4. openat与open的区别及用法示例

    从2.6.16版本开始,GNU/Linux引入opeant系统调用: #define _XOPEN_SOURCE 700 /* Or define _POSIX_C_SOURCE >= 2008 ...

  5. Git Bash输错账号密码如何重新输入

    很多时候我们容易在Git Bash操作的时候,不慎输入错误的用户名或密码,此时一直提示: remote: Incorrect username or password ( access token ) ...

  6. indows Eclipse Scala编写WordCount程序

    Windows Eclipse Scala编写WordCount程序: 1)无需启动hadoop,因为我们用的是本地文件.先像原来一样,做一个普通的scala项目和Scala Object. 但这里一 ...

  7. 运算符优先级 以及 && (逻辑与) 和||(逻辑或)的优先级:

    运算符优先级(从高到低列出) 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型. ...

  8. 迁移WordPress

    一.迁移目的 WordPress是一款能让您建立出色网站.博客或应用的开源软件.官网:https://cn.wordpress.org/download/,最开始是将WordPress部署在本地虚拟机 ...

  9. ArrayList插入1000w条数据的时间比较分析

    一分钟系列: 读懂GC日志 ArrayList插入1000w条数据之后,我怀疑了jvm... Java JIT性能调优 Java性能优化指南系列(三):理解JIT编译器 准备:调试程序加入VM Opt ...

  10. Winform界面GridView中XCDataGridViewCheckBoxAllColumn改变触发事件

    1.首先利用CurrentCellDirtyStateChanged事件 监测状态改变后判断是否有未提交的更改,若有则提交 private void CurrentCellDirtyStateChan ...