Access viewchild from another component
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?
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的更多相关文章
- [Angular] @ViewChild read custom directive and exportAs
For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...
- [Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view child ...
- Angular 2 中的 ViewChild 和 ViewChildren
https://segmentfault.com/a/1190000008695459 ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfter ...
- React.js Tutorial: React Component Lifecycle
Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...
- 登陆peoplesoft的时候显示信息
Signon Event Message Select selectPeopleTools, then selectUtilities, then selectAdministration, then ...
- eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法
当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...
- hibernate学习(一)配置,导包
框架的作用 学过javaWeb基础的已经对web层 jsp servlet ,service 层 ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...
- salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型
看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...
- Ionic2 渐变隐藏导航栏|标题栏
废话少说 直接上代码.... //导入需要用到的命名空间 ViewChild,Content import { Component, ViewChild } from '@angular/core'; ...
随机推荐
- 重置mysql数据库root密码
一. 在已知MYSQL数据库的ROOT用户密码的情况下,修改密码的方法:1,shell环境下:]#mysqladmin –u root –p password “新密码” 回车后要求输入旧密码2,my ...
- 【算法】矩阵填数,深度优先搜索(DFS),Pascal改C语言
面向对象的上机实验 题目 以下列方式向 5*5 矩阵中填入数字.设数字i(1=<i<=25),则数字i+1 的坐标位置应为(E, W).(E, W)可根据下列关系由(x,y)算出: 1)( ...
- centOS 安装 npm
下载 cd /usr/local/node wget https://npm.taobao.org/mirrors/node/v10.14.1/node-v10.14.1-linux-x64.tar. ...
- jquery(第一章)认识jquery
Jquery对象就是通过JQuery包装DOM对象后产生的对象,在JQuery对象中无法使用DOM对象的任何方法. 1.4.2 JQuery对象的和DOM对象的互相转换 若获取的对象是JQuery对象 ...
- Nginx05---负载均衡 upsteam
参考 https://www.cnblogs.com/linjiqin/p/5494783.html
- request方法
获取请求行方法: getMethod()获取请求的方法 getContextPath()回去虚拟路径 getServletPath()获取路径(只有在servert中使用) getQueryStrin ...
- java--键盘输入任意数字进行求和
思路,我将键盘输入的数放入数组,然后便利数组进行求和 package com.test.day01; import java.util.Scanner; public class Test { pub ...
- 基础python规范
一.注释 合理的代码注释应该占源代码的 1/3 左右,Python 语言允许在任何地方插入空字符或注释,但不能插入到标识符和字符串中间. 在 Python 中,通常包括 3 种类型的注 ...
- ubuntu+nginx+uwsgi部署django web项目
前言 将本地开发的django项目部署至linux上的uwsgi服务器,并配置nginx,完成基于ubuntu+nginx+uwsgi的上线运行.下面整理相关步骤. 服务器配置virtualenv 如 ...
- linux——环境变量
环境变量 基本概念: 一般是指在操作系统中用来指定操纵系统运行环境的一些参数 当我们用动态库链接成功的时候,其实就是相关的环境变量帮助编译器进行查找. 环境变量通常具有某种特殊用途,还有在系统当中通常 ...
angular