加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2的组件传值Input和Output

发布时间:2020-12-17 09:32:28 所属栏目:安全 来源:网络整理
导读: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=[]; constructo
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读