Angular中@Output()的使用方法

子component中的html文件

<button (click)="Send()">送出</button><br>

子component中的ts文件

复制代码
import { Component, OnInit, Output } from '@angular/core';
import { EventEmitter } from '@angular/core';

@Component({
  selector: 'app-todo-input',
  templateUrl: './todo-input.component.html',
  styleUrls: ['./todo-input.component.css']
})
export class TodoInputComponent implements OnInit {
  public content: string;
  @Output() onSend: EventEmitter<string> = new EventEmitter<string>();
  constructor() { }

  ngOnInit(): void {
    this.content = "Hello";
  }

  Send(){
    this.onSend.emit(this.content);
  }
}
复制代码

父component中的html

<app-todo-input (onSend)="onSend($event)"></app-todo-input>

父component中的ts

复制代码
import { Component, OnInit } from '@angular/core';
import {Todo} from '../../models/todo'

@Component({
  selector: 'app-todo',
  templateUrl: './todo.component.html',
  styleUrls: ['./todo.component.css']
})
export class TodoComponent implements OnInit {

  public List:Todo[]=[];

  constructor() { }

  ngOnInit(): void {
    // this.List = [ 
    //   {id:1,content:'Test'},
    //   {id:2,content:'Test2'},
    //   {id:3,content:'Test3'},
    // ];
  }
  onSend(content: string){
    alert(content);
  }

}
复制代码

 



posted @   Magi黄元  阅读(618)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示