大佬教程收集整理的这篇文章主要介绍了angular2的组件传值Input和Output,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import { Component,OnInit } from '@angular/core'; @Component({ SELEctor: 'app-parent',templateUrl: './parent.component.html',styleUrls: ['./parent.component.css'] }) export class ParentComponent implements OnInit { contacts:any=[]; constructor() { } ngOnInit() { this.contacts=[ {title:'s',good:1},{title:'ss',good:11},{title:'sss',good:111},{title:'ssss',good:1111} ]; } childOutEvent(ev,i){ this.contacts[i].good += ev; } }
<p> parent works! </p> <ul> <li *ngFor="let contact of contacts;let i = index"> <app-child [contact]="contact" (OutEvent)="childOutEvent($event,i)" ></app-child> </li> </ul>
import { Component,OnInit,Input,Output,EventEmitter } from '@angular/core'; @Component({ SELEctor: 'app-child',templateUrl: './child.component.html',styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnInit { @input() contact:any={}; @Output() OutEvent = new EventEmitter<number>(); constructor() { } ngOnInit() { } good_to_parent(num){ this.OutEvent.emit(num); } }
{{Contact.titlE}} - {{Contact.gooD}} <button (click)="good_to_parent(1)" >good +1</button> <button (click)="good_to_parent(-1)" >good -1</button>
https://angular.cn/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child
以上是大佬教程为你收集整理的angular2的组件传值Input和Output全部内容,希望文章能够帮你解决angular2的组件传值Input和Output所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。