zoukankan      html  css  js  c++  java
  • angular父子组件传值

    angular 组件传值---****---子传父。。。。@Output
    *****子组件
    --------ts文件
    import {Component, Output, EventEmitter,OnInit} from '@angular/core';
    @Component({
    selector: 'child-Component',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.less']
    })
    export class childComponent implements OnInit {
    //使用"事件传递"是 "子组件" 向 "父组件" 传递数据最常用的方式,子组件需要实例化EventEmitter类来订阅和触发自定义事件
    @Output() childDataevent = new EventEmitter();//自定义事件 实例化EventEmitter,赋值给event,event被@Output装饰器定义为输出属性,
    private username: string; // 这样event具备了向上级传递数据的能力,通过调用EventEmitter类中定义的emit方法,来向上传递数据

    constructor() {}
    ngOnInit(): void {}
    submitVal(){
    this.childDataevent.emit(this.username);
    return
    }
    }
    -----html文件
    用户名<input nz-input [(ngModel)]="username" name="username" type="text">
    <button (click)="submitVal()">提交--子传父</button>
    *****父组件
    --------ts文件
    import { Component, OnInit } from '@angular/core';
    @Component({
    selector: 'logoSing',
    templateUrl: './logoSing.html',
    styleUrls: ['./logoSing.css']
    })
    export class LogoSingComponent implements OnInit {
    private parent_username: string;
    constructor() { }
    ngOnInit() { }
    getData(event) {
    this.parent_username = event;
    }
    }
    --------html文件
    子组件展示在父组件的值:{{parent_username}}
    <child-Component (childDataevent)="getData($event)"></child-Component>

    angular 组件传值---****---父传子。。。。@Input


    *****父组件

    ********子组件

    *******父html

    *****子组件

  • 相关阅读:
    Q:简单实现URL只能页面跳转,禁止直接访问
    Q:elementUI中tree组件动态展开
    一个切图仔的 JS 笔记
    一个切图仔的HTML笔记
    一个切图仔的 CSS 笔记
    GnuPG使用笔记
    SQL Svr 2012 Enterprise/Always-on节点连接超时导致节点重启——case分享
    网卡配置文件备份在原目录下引起网络配置异常
    python培训
    service脚本的写法
  • 原文地址:https://www.cnblogs.com/lihong-123/p/9987316.html
Copyright © 2011-2022 走看看