angular 4+中关于父子组件传值的示例
home.component.ts
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit { constructor() { } ngOnInit() {
} app_substring: string = "<app-subcomponet [msg]='msg' [parentFun]='parentFun'></app-subcomponet>";
app_substringtwo:string="在parent.component的模板中使用 <app-subcomponet (childbroadcastevent)=parentsubcribe($event)"; }
header.component.html <!-- <p>header works!</p> -->
<h1 class="header">this is {{msg}}</h1>
<hr />
<button (click)='childFun()'>调用父组件的方法</button>
<button (click)='childOutFun()'>子组件广播事件向父组件传值</button>
<hr />
<!-- ----------------header data--------------------
{{title}}---{{msg}} -->
header.component.ts
import { Component, OnInit, Input, OnDestroy, OnChanges, Output, EventEmitter } from '@angular/core';
// import { EventEmitter } from 'protractor';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit, OnDestroy, OnChanges {
title = 'this is header\'s title';
//模板参数定义方式,注意,input传入的是对象,即可以是属性,还可以是方法
///接收父组件的属性
@Input() msg: string;
@Input() parentFun: any;
@Output() childOut = new EventEmitter<string>();
message: string;
constructor() {
console.log('constructor...' + this.msg);
}
ngOnInit() {
// this.msg = 'this is header data'
console.log('ngOnInit...' + this.msg);
}
ngOnDestroy() {
console.log('ngOnDestroy...' + this.msg);
}
ngOnChanges() {
console.log('ngOnChanges...' + this.msg);
}
childFun() {
this.message = '这是header传给父组件的参数';
this.parentFun(this.message);
}
///子组件定义一个output的属性,来作为父组件的引用,进行广播
///父组件通过,监听这样一个子组件的广播属性,来响应子组件的操作 注意:由于是事件,所以用 ()
childOutFun() {
this.childOut.emit('子组件向父组件广播的emitdata');
}
childFunForParent() {
alert('供父组件调用的方法');
}
}
news.component.ts
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-news',
templateUrl: './news.component.html',
styleUrls: ['./news.component.css']
})
export class NewsComponent implements OnInit, AfterViewInit {
@ViewChild('headerchild', { read: '', static: true }) headerchild;
public news: any[];
protected spanhtml: string = '<h2>测试显示HTML代码,是否能解析成功</h2>';
protected id: Number = 100;
public search: string = '';
public msg: string = '这是news的Header';
constructor(private http: HttpClient) {
this.news = [
{
'cateName': '宝马',
'brand': [{ 'title': '宝马X1' },
{ 'title': '宝马X1' },
{ 'title': '宝马X2' },
{ 'title': '宝马X3' },
{ 'title': '宝马X4' }]
},
{
'cateName': '奥迪',
'brand': [{ 'title': '奥迪q1' },
{ 'title': '奥迪q2' },
{ 'title': '奥迪q3' },
{ 'title': '奥迪q4' },
{ 'title': '奥迪q5' }]
},
{
'cateName': '中华',
'brand': []
}
];
}
ngOnInit() {
}
ngAfterViewInit() {
}
getApiData() {
let url: string = 'https://itunes.apple.com/search';
let searchUrl = `${url}?term=${this.search}&media=music&limit=20`;
this.http.jsonp(url, 'callback').subscribe(
function (data) { console.log(data) },
function (error) { console.log(error) })
}
///父组件通过属性传值公开给子组件的方法
parentFun(msg) {
alert(msg);
}
///父组件监听的子组件方法
parentAimedFun(e) {
alert(e);
}
////父组件通过viewchild直接调用子组件的方法
excuteChildFunByViewChild() {
this.headerchild.childFunForParent();
}
}
news.component.html <app-header [msg]='msg' [parentFun]='parentFun' (childOut)='parentAimedFun($event)' #headerchild></app-header> <button (click)="headerchild.childFunForParent()">试试父组件执行子组件方法</button>
<button (click)="excuteChildFunByViewChild()">试试父组件通过viewchild执行子组件方法</button> <br>
<p>news works!</p>
<span [innerHTML]="spanhtml"></span>
<div [id]="id"></div> <div>
<ul>
<li *ngFor="let item of news;let key=index">
系列:{{item.cateName}}<br>
车型数量:{{item.brand.length}}
<ol>
<div *ngIf="item.brand.length>0">
<li *ngFor="let car of item.brand;let key=index">
车型:{{car.title}}
</li>
</div>
<div *ngIf="item.brand.length==0">
暂无更多数据
</div>
</ol>
</li>
</ul>
</div> <hr />
<h2>Jsonp查询数据</h2>
输入关键字:<input type="text" [(ngModel)]="search" />
<button (click)="getApiData()">点击查询</button>
angular 4+中关于父子组件传值的示例的更多相关文章
- Angular 父子组件传值
Angular 父子组件传值 @Input @Output @ViewChild 新建一个头部组件 newsheader 在主组件引用 news 组件,在news组件添加 newsheader 组 ...
- 关于element中的父子组件的传值问题
首先讲一下父子组件的传值问题. 这是大家很熟悉的一张图,讲述了父子组件传值的过程.父组件通过prop传值给子组件,子组件通过$emit给父组件发送消息来使父组件的prop发生变化.这都是老生常谈了.下 ...
- 十八、React react-router4.x中:实现路由模块化、以及嵌套路由父子组件传值
一.路由模块化(用字典定义路由,然后循环出来) 1.官方文档参考 [官方文档]https://reacttraining.com/react-router/web/guides/quick-start ...
- 使用react进行父子组件传值
在单页面里面,父子组件传值是比较常见的,之前一直用vue开发,今天研究了一下react的父子组件传值,和vue差不多的思路,父组件向子组件传值,父通过初始state,子组件通过this.props进行 ...
- 【vue】父组件主动调用子组件 /// 非父子组件传值
一 父组件主动调用子组件: 注意:在父组件使用子组件的标签上注入ref属性,例如: <div id="home"> <v-header ref="he ...
- vue父子组件传值加例子
例子:http://element-cn.eleme.io/#/zh-CN/component/form 上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- Vue中非父子组件传值的问题
父子组件传值的问题,前面已经讲过,不再叙述,这里来说一种非父子组件的传值. vue官网指出,可以使用一个空vue实例作为事件中央线! 也就是说 非父子组件之间的通信,必须要有公共的实例(可以是空的), ...
- 创建组件的方法,组件的props属性、state属性的用法和特点,父子组件传值,兄弟组件传值
1.创建组件的方法 函数组件 class组件 1.1 函数组 无状态函数式组件形式上表现为一个只带有一个 `render()` 方法的组件类,通过函数形式或者 `ES6` 箭头 `functi ...
随机推荐
- C++入门经典-例9.2-重载函数模板,求出字符串的最小值
1:整形数和实型数编译器可以直接进行比较,所以使用函数模板后也可以直接进行比较,但如果是字符指针指向的字符串该如何处理呢?这时可以通过重载函数模板来实现.通常字符串需要库函数来进行比较,通过重载函数模 ...
- CodeForces 137A
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Polyca ...
- [windows菜鸟]C#中调用Windows API参考工具
很多windows API都不知道签名,可以从下面几种方式进行查询 1.微软出的工具 P/Invoke Interop Assistant version 1.0 2.网站 pinvoke.net 3 ...
- is幻梦 Linux命令之文件和目录操作命令(二)——查看文件内容cat、more、less、tail、head
一.cat命令 用法:cat [选项][文件] 1.主要用来查看文件内容 2.-n 在文件内容的每一行上加上行号 3.再不加任何选项和文件名的时候,将标准输入的内容复制到标准输出 这个时候一般用于重定 ...
- Tomcat 8.5 配置自动从http跳转https
1.需要修改Tomcat目录下的conf/server.xml文件两处地方,Http端口,从默认的8080改为80:Https端口从8443改为443:(80\443分别都是http/https默认端 ...
- iOS证书详解--转载
一.成员介绍1. Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1) Developer Certification(开发证书)安装在电脑上 ...
- Linux0.11之进程0创建进程1(1)
进程0是由linus写在操作系统文件中的,是预先写死了的.那么进程0以后的进程是如何创建的呢?本篇文章主要讲述进程0创建进程1的过程. 在创建之前,操作系统先是进行了一系列的初始化,分别为设备号.块号 ...
- golang(11) 反射用法详解
原文链接:http://www.limerence2017.com/2019/10/14/golang16/ 反射是什么 反射其实就是通过变量动态获取其值和类型的一种技术,有些语言是支持反射的比如py ...
- C基础知识(3):指针--概念、数组中指针的递增/递减、指针数组&数组指针、指向指针的指针
指针是一个变量,其值为另一个变量的地址. 所有指针的值的实际数据类型,不管是整型.浮点型.字符型,还是其他的数据类型,都是一样的,都是一个代表内存地址的长的十六进制数. 下面从4个代码例子分别讲述以下 ...
- 【HANA系列】【第五篇】SAP HANA XS的JavaScript API详解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第五篇]SAP HANA XS ...