[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 ...
随机推荐
- 数据库操作封装类 DBHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- (转)\r \r\n \t 的区别
小风吹雪 \r \r\n \t 的区别 http://www.360doc.com/content/12/0530/15/16538_214756101.shtml \n 软回车: 在Wi ...
- Cacti添加Advance Ping监控模板
Cacti脚本 1.Advance Ping 脚本及模板 注:要使用此模板,编译PHP时必须加上--enable-sockets选项来支持套接字. 1).功能:此模板用来监控一个TCP/U ...
- msi软件包无法安装
安装某些msi软件包,提示“This advertised application will not be installed because it might be unsafe. Contact ...
- Ubuntu 11.10开启root用户登陆
以管理员身份运行 #sudo gedit /etc/lightdm/lightdm.conf 将里面改成 "autologin-user=root" 就可以以root用户登录了
- ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地
SQL> alter database open; *第 1 行出现错误:ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地ORA-00312: 联机日志 1 线程 1 ...
- 利用谷歌 kaptcha 进行验证码生成
package main.com.smart.controller; import com.google.code.kaptcha.Producer; import main.com.smart.ut ...
- Win+PHP+IECapt完整实现网页批量截图并创建缩略图
最近在开发一个本地互联网应用的项目,为了增加用户体验,需要在搜索结果左侧显示如图一所示的某个网站的缩略图效果,在网上不停地百度谷歌了一上午后,发现大多数实现少量截图还是可以的,如果大批量的截图总会在中 ...
- FlowPlayer 参数说明
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> & ...
- Install FFmpeg, Mplayer, Mencoder, MP4Box, Flvtool2
You can use the following tutorial to install ffmpeg and other video modules in your centos server.F ...