[Angular 2] @ViewChild to access Child component's method
When you want to access child component's method, you can use @ViewChild in the parent:
Parent Component:
import {Component, OnInit, ViewChild} from 'angular2/core';
import {HeroService, Hero} from './HeroService';
import {Observable} from 'rxjs/Rx';
import {SelectedHero} from './selected-hero';
import {HeroItem} from './hero-item';
@Component({
selector: 'hero-list',
directives: [SelectedHero, HeroItem],
template: `
<button (click)="removeSelectedHero()">clear</button>
<ul>
<li *ngFor="#hero of heros | async">
<hero-item [hero]="hero" (changed)="thisHero = $event"></hero-item>
</li>
</ul>
<selected-hero [thisHero]="thisHero"></selected-hero>
`
})
export class HeroList implements OnInit{
heros: Observable<Hero[]>;
@ViewChild(SelectedHero) selectedItem: SelectedHero;
constructor(public heroService: HeroService){}
ngOnInit(){
this.getHeros();
}
removeSelectedHero(){
this.selectedItem.clear();
}
getHeros(){
this.heros = this.heroService.getHeros();
}
}
Child Component:
import {Component, Input} from 'angular2/core';
import {Hero} from './HeroService';
@Component({
selector: 'selected-hero',
template: `
<h2>{{thisHero && thisHero.name}}</h2>
`
})
export class SelectedHero{
@Input() thisHero: Hero;
constructor(){
}
clear(){
this.thisHero = new Hero("");
}
}
[Angular 2] @ViewChild to access Child component's method的更多相关文章
- Angular 2 ViewChild & ViewChildren
一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...
- e621. Activating a Keystroke When Any Child Component Has Focus
Normally, a keystroke registered on a component is activated when the component has the focus. This ...
- [Vue] Parent and Child component communcation
By building components, you can extend basic HTML elements and reuse encapsulated code. Most options ...
- vue & child component & props
vue & child component & props vue pass data to child component https://vuejs.org/v2/guide/co ...
- [Angular 2] Generate and Render Angular 2 Template Elements in a Component
Angular 2 Components have templates, but you can also create templates inside of your templates usin ...
- [Angular 2] Building a Toggle Button Component
This lesson shows you how to build a Toggle Button in Angular 2 from scratch. It covers using transc ...
- Angular利用@ViewChild在父组件执行子组件的方法
代码如下: @Component({ selector: 'my-app', template: ` <step-bar #stepBar></step-bar> ` }) e ...
- 从flask视角理解angular(二)Blueprint VS Component
Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
随机推荐
- Arcgis Android 基本概念 - 浅谈
MapView MapView 是 Android 中 ViewGroup的子类,也是 ArcGIS Runtime SDK for Android 中的地图容器,与很多 ArcGIS API ...
- [c#]asp.net开发微信公众平台(1)数据库设计
开发微信公众平台之前,先去微信官方了解下大概的情况 这里:http://mp.weixin.qq.com/wiki/index.php :看了之后心里大致有数了,开始设计数据库,尽可能的考虑,未考虑到 ...
- 无法连接vCenter Server清单https://IP:10443
VMware vCenter Server服务器安装系统的时候使用一个IP,安装完VMware vCenter后来更换了另外一个IP,当使用vSphere Web Client登陆VMware vCe ...
- JSP中取COOKIE中指定值得方法【转载】
Cookie cookies[]=request.getCookies(); //读出用户硬盘上的Cookie,并将所有的Cookie放到一个cookie对象数组里面 Cookie sCookie=n ...
- Apache的配置
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...
- Hibernate对象的状态和映射
一. Hibernate对象的状态 实体对象的三种状态: 1) 暂态(瞬时态)(Transient)---实体在内存中的自由存在,它与数据库的记录无关. po在DB中无记录(无副本),po和sessi ...
- 启动任务StartTask() 发送完消息队列 自己删除,接收方一直显示数据 用OSQFlush(Str_Q); //清空消息队列 下面纠结接收不到了 哈哈
在建立工程的时候,启动任务StartTask() 启动了任务MyTask(),也建立了消息队列,然后发送消息队列,发送完自己删除了自己,在接收方一直能接受到数据???为何??? 因为我们的消息队列未 ...
- css的!important规则对性能有影响吗
最近在做项目中发现很多CSS代码里面都使用!important去覆盖原有高优先级的样式.按照常理来说,越是灵活的东西,需要做的工作就会更多.所以想当然的认为像!important这样灵活.方便的规则如 ...
- Javascript Array Distinct (array.reduce实现)
javascript 没有原生的Distinct功能 . (至少现在还没有)但我们可以通过简单的script 自己实现 . Distinct就是把数组中重复出现2次或以上的值给删除掉,确保数组内每个值 ...
- 抽象类的基本概念------abstract
抽象类的概念: 包含一个抽象方法的类就称为抽象类. 抽象方法:只声明但未实现的方法称为抽象方法,使用abstract关键字声明. 抽象类的定义及使用规则: abstract class A{ // 是 ...