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的更多相关文章

  1. Angular 2 ViewChild & ViewChildren

    一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...

  2. 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 ...

  3. [Vue] Parent and Child component communcation

    By building components, you can extend basic HTML elements and reuse encapsulated code. Most options ...

  4. vue & child component & props

    vue & child component & props vue pass data to child component https://vuejs.org/v2/guide/co ...

  5. [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 ...

  6. [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 ...

  7. Angular利用@ViewChild在父组件执行子组件的方法

    代码如下: @Component({ selector: 'my-app', template: ` <step-bar #stepBar></step-bar> ` }) e ...

  8. 从flask视角理解angular(二)Blueprint VS Component

    Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...

  9. [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 ...

随机推荐

  1. Android - Error parsing XML: unbound prefix

    概述 这个问题,虽然看起来不是问题,但是如果不知道的人,还会花点时间,有的人甚至重新安装ADT. 我一开始还以为是排版的问题(Layout),因为初学,弄来弄去,最好还是到网上搜. 其实就不是什么问题 ...

  2. Objective-C学习篇09—NSNumber与笑笑语法

    NSNumber 由于数组,字典,集这三个容器中只能存放对象类型的数据,如果想把基本数据类型的数据存放到这三个容器中,需要把基本数据类型转化为对象类型,此时就要借助于NSNumber 这个类. NSN ...

  3. PHP Calendar 函数

    PHP 5 Calendar 函数 函数 描述 cal_days_in_month() 针对指定的年份和历法,返回一个月中的天数. cal_from_jd() 把儒略日计数转换为指定历法的日期. ca ...

  4. 分享动态拼接Expression表达式组件及原理

    前言 LINQ大家都知道,用起来也还不错,但有一个问题,当你用Linq进行搜索的时候,你是这样写的 var query = from user in db.Set<User>()      ...

  5. 通过枚举enum实现单例设计

    一.枚举 通过enum关键字来实现枚举,在枚举中需要注意的有: 1. 枚举中的属性必须放在最前面,一般使用大写字母表示 2. 枚举中可以和java类一样定义方法 3. 枚举中的构造方法必须是私有的 通 ...

  6. Hibernate 缓存机制(转)

    一.why(为什么要用Hibernate缓存?) Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数 ...

  7. 无缝滚动js (手写通俗易懂)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. sql错误代码一览表

    http://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm Table 8-3. SQL-92 SQLSTATE Return Codes Cla ...

  9. jQuery制作go to top按钮

    转自:http://www.w3cplus.com/jquery/scrolling-to-the-top-with-jquery 每每看到网友Blog的页面底部或中间有一个按钮回到页面顶部,羡慕死人 ...

  10. 学习微信小程序之css17clearfix原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...