ng2父子模块通信@ViewChild和@Inject
一、@ViewChild
父组件中使用@ViewChild拿到子组件的变量和方法(父组件可调用子组件的方法和变量)
parent.component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';
@Component({
selector: 'my-parent',
templateUrl: './parent.component.html',
styleUrls: [ './parent.component.css' ],
})
export class ParentComponent implements OnInit {
//通过@ViewChild注册子组件
@ViewChild(ChildComponent) public child:ChildComponent;
public countNum: number;
public firstName:string = "Jeck";
public fullName:string = "";
constructor() {}
ngOnInit(): void {
}
displayFull(){
this.fullName = this.firstName + this.child.lastName;
console.log(this.fullName) //"Jeck wang"
}
}
child.component.ts:
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'my-child',
templateUrl: './child.component.html',
styleUrls: [ './child.component.css' ],
})
export class ChildComponent implements OnInit {
public lastName:string = "wang";
constructor() {}
ngOnInit(): void {
}
}
二、@Inject
子组件中使用@Inject调用父组件中的变量和方法
parent.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-parent',
templateUrl: './parent.component.html',
styleUrls: [ './parent.component.css' ],
})
export class ParentComponent implements OnInit {
constructor() {}
ngOnInit(): void {
}
sayHello(){
console.log("Hello!")
}
}
child.component.ts:
import { Component, OnInit, Inject, forwardRef} from '@angular/core';
import { ParentComponent } from './parent.component';
@Component({
selector: 'my-child',
templateUrl: './child.component.html',
styleUrls: [ './child.component.css' ],
})
export class ChildComponent implements OnInit {
constructor(
@Inject(forwardRef(()=>ParentComponent)) public parent:ParentComponent
) {}
ngOnInit(): void {
this.parent.sayHello(); //"Hello!"
}
}
注意:如果父子模块通过以上方式相互引用,请在父模块中使用 @ViewChild(forwardRef(()=>ChildComponent)) public child:ChildComponent 方式避免父子组件循环引用报错
ng2父子模块通信@ViewChild和@Inject的更多相关文章
- ng2父子模块数据交互
一.父模块向子模块传值 //父html <my-child [childdata]="parentdata"></my-child> 其中,my-child ...
- Vue 非父子组件通信方案
Vue 非父子组件通信方案 概述 在 Vue 中模块间的通信很普遍 如果是单纯的父子组件间传递信息,父组件可以使用 props 将数据向下传递到子组件,而在子组件中可以使用 events (父组件需要 ...
- 三大前端框架(react、vue、angular2+)父子组件通信总结
公司业务需要,react.vue.angular都有接触[\无奈脸].虽然说可以拓展知识广度,但是在深度上很让人头疼.最近没事的时候回忆各框架父子组件通信,发现很模糊,于是乎稍微做了一下功课,记录于此 ...
- 从$emit 到 父子组件通信 再到 eventBus
故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...
- vue组件通信之父子组件通信
准备工作: 首先,新建一个项目,如果这里有不会的同学,可以参考我转载过的文章 http://www.cnblogs.com/Sky-Ice/p/8875958.html vue 脚手架安装及新建项目 ...
- 总结Vue第二天:自定义子组件、父子组件通信、插槽
总结Vue第二天:自定义子组件.父子组件通信.插槽 一.组件: 组件目录 1.注册组件(全局组件.局部组件和小demo) 2.组件数据存放 3.父子组件通信(父级向子级传递数据.子级向父级传递数据) ...
- 关于React的父子组件通信等等
//==================================================此处为父子组件通信 1.子组件调用父组件: 父组件将子组件需要调用方法存入props属性内,子组 ...
- Vue 非父子组件通信
组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求.组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信.前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有 ...
- vue父子组件通信
一.父子组件间通信 vue.js 2.0提供了一个ref 的属性: 可以为子组件指定一个索引id 父组件: <template> <div id='user-login'> & ...
随机推荐
- Jquery获取iframe中的元素
iframe与父页面之间相互获取元素的方法: 1.从父页面中获取iframe页面中的元素: 用法: $(window.frames["iframe_include_adverse" ...
- windows 2008配置运行PHP5.5.X
1.安装web5.0平台安装程序.web5.0平台安装程序:http://www.iis.net/downloads (实际上更方便的是用WebPlalform安装PHP:http://www.mic ...
- [Python]Pip换源以及设置代理
Install python package with proxy sudo pip install python-magic --proxy=https://your-proxy.com 2.No ...
- string 转 LPCTSTR
(1)在ANSI字符集下 LPCTSTR想当于LPCSTR,当中L指long.P指Point,C指Const.在程序中能够直接用char*类型的数据对LPCSTR进行赋值,用下述语句: LPCSTR ...
- 利用framebuffer,命令行显示图片
上代码 import fcntl import struct import mmap import contextlib import os import time import numpy as n ...
- <raspberry pi > 用树莓派来听落网电台
树莓派放在抽屉里吃灰有半年多了,去年玩了1个月后就没怎么开整了,上个月没工作,刚好有点闲暇,就把树莓派翻出来折腾,刚好碰到落网改版了,想起以前在树莓派论坛看到有网友拿树莓派来听豆瓣电台,代码那时我都下 ...
- Java对象 的创建与构造方法
一.创建对象的四种方法: a. new语句: b. 利用反射,调用描述类的Class对象的newInstance()实例方法: c. 调用对象的clone(): d. 反序列化: 其中new 和 ne ...
- <算法笔记>关于快速排序的算法优化排序(顺便给百度百科纠个错)
快速排序是排序算法之中的基本中的基本,虽然越来越多的接口函数将快速排序“完美的封装了起来”,比如C++中的qsort或者<algorithm>中的sort(与stable_sort相对应) ...
- Oracle 数据库SQL
原作者:http://blog.csdn.net/jihuanliang/article/details/7205968 总体说说可能出现的原因: 情况场景: 表A中有个字段是外键,关联了表B中的某字 ...
- ultraedit激活
使用期满的解决办法:https://blog.csdn.net/dfh00l/article/details/52093630 下载:https://blog.csdn.net/qq_16093323 ...