zoukankan      html  css  js  c++  java
  • 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);
      }
    
    }
    
    
    
    
  • 相关阅读:
    java中怎么跳出两层for循环
    卡斯特信号有限公司面经
    唯一索引、普通索引、主键索引的区别
    ES中的查询操作
    sql:union 与union的使用和区别
    Java中多个集合的交集,并集和差集
    Angular动态组件
    Angular惰性加载的特性模块
    spring定时器
    索引
  • 原文地址:https://www.cnblogs.com/magicg/p/15341407.html
Copyright © 2011-2022 走看看