angular2之@Output() EventEmitter

A

src/app/components/contains/contain1.ts

import { Component,Output ,EventEmitter} from '@angular/core';
@Component({    
selector: 'contain1',    
template: `        
<div>            
  <h3> contain1 </h3>            
  <div (click)="onChecked()" >                
    <button value="123"></button>            
  </div>        
</div>       
`})
export class Contain1 {    
note = 'EventEmitter test'    
@Output() checked = new EventEmitter();    
onChecked(){        
  this.checked.next("next:"+this.note);  //过时  
             
  this.checked.emit("emit:"+this.note);    
}}

B

src/app/app.component.ts

import { Component } from '@angular/core';
import '../../public/css/styles.css';
import { Contain1,Contain2 } from './components/contains'
@Component({  
selector: 'my-app',  
directives:[    Contain1,Contain2  ],  
template:`        
  <contain1 (checked)="showChecked($event)"></contain1>  

  <contain2></contain2>    `,  
styles: [require('./app.component.css')]})
export class AppComponent {  
  showChecked(note:String){   
     console.log(note);     
 }}

说明

A :

  • @Outpout 定义一个输出
  • onChecked(), A 中的自定义方法,通过emit,触发@Outpout

B: 使用 A 中定义的 @Output(),$event 必须,$event 是B 中通过emit 传过来的。



作者:CK110
链接:https://www.jianshu.com/p/f2768f927c86
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值