angular4父组件向子组件传值,子组件向父组件传值的方法

angular4父组件向子组件传值,子组件向父组件传值的方法父组件向子组件传值@Input文件目录父组件:father.template.html<h1>父组件</h1><cmt-child[data]=’data’></cmt-child>father.component.tsimport{Component,OnIn…

大家好,又见面了,我是你们的朋友全栈君。

父组件向子组件传值 @Input

文件目录

1250245-20181019162504929-1031754682.png

父组件:

father.template.html

<h1>父组件</h1>
<cmt-child [data]='data'></cmt-child>

father.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'cmt-father',
    templateUrl: './father.template.html'
})
export class FatherComponent implements OnInit {
    data: any = '我是传往子组件的值'
    ngOnInit() {
    }

    ngOnChanges() {
    }

}

子组件:(使用@Input修饰器去接收)

childcomponent.ts

import { Component, OnInit, Input } from '@angular/core';

@Component({
    selector: 'cmt-child',
    templateUrl: './child.template.html'
})
export class ChildComponent implements OnInit {
    @Input() data: any;//接收父组件的值
    ngOnInit() {
        console.log(this.data)
    }

    ngOnChanges() {
        console.log(this.data)
    }

}

这样就把值从父组件传到了子组件!

子组件向父组件传值(子组件通过方法借助修饰器@output传值给父组件)

子组件

childcomponent.ts

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
    selector: 'cmt-child',
    templateUrl: './child.template.html'
})
export class ChildComponent implements OnInit {
    @Output('checked') checkedBack = new EventEmitter<any>();
    id:any ="我是传给父组件的值"
    ngOnInit() {
    }

    ngOnChanges() {
    }
    checkedCallback() {
        this.checkedBack.emit(this.id);
    }
}

child.template.html.html

<h1>子组件</h1>
<button (click)='checkedCallback()'>点击传值给父组件</button>

父组件

father.template.html

<h1>父组件</h1>
<cmt-child (checked)="checkedBack($event)"></cmt-child>

father.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'cmt-father',
    templateUrl: './father.template.html'
})
export class FatherComponent implements OnInit {
    ngOnInit() {
    }

    ngOnChanges() {
    }
    
    checkedBack(event) {
        console.log(event)
    }
}

这样子组件通过点击就把值传递给了父组件!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141665.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • route 命令详解

    route 命令详解转自“ITPUB博客”,链接:http://blog.itpub.net/86584/viewspace-755197/因为工作中需要了解网络配置,在众多文中,找到一详解,特此分享。1.使用背景需要接入两个网络,一个是部署环境所在内网环境,这个环境是上不了外网,外网环境很可能是一个无线网络。如果两者都连接上,很可能导致有一方不能起作用,即外网或内网上不了,常常需要…

    2022年7月18日
    13
  • matlab中画柱状图的函数_科学柱状图怎么画

    matlab中画柱状图的函数_科学柱状图怎么画论文中需要画图进行比较,感觉还是matlab画起来比较方便,先把自己画的图及matlab代码放上。y=[300311;390425;312321;250185;550535;420432;410520;];b=bar(y);gridon;ch=get(b,’children’);set(gca,’XTickLabel’,{‘0′,’1′,’2′,’3’,’…

    2022年10月19日
    0
  • 死锁的四个必要条件和解决办法_半暖的博客_活锁和死锁的概念

    死锁的四个必要条件和解决办法_半暖的博客_活锁和死锁的概念死锁概念及产生原理   概念:多个并发进程因争夺系统资源而产生相互等待的现象。   原理:当一组进程中的每个进程都在等待某个事件发生,而只有这组进程中的其他进程才能触发该事件,这就称这组进程发生了死锁。   本质原因:     1)、系统资源有限。     2)、进程推进顺序不合理。死锁产生的4个必要条件  1、互斥:某种资源一次只允许一个进程访问,即该资源一旦分配给某个进程…

    2022年4月20日
    62
  • python3两数相加

    python3两数相加

    2021年4月18日
    135
  • jenkins+maven +svn+tomcat7集群部署(一)

    jenkins+maven +svn+tomcat7集群部署(一)

    2021年12月5日
    53
  • QueryInterface 实现及使用的完整的例子

    QueryInterface 实现及使用的完整的例子下面我们将把前面所提到过和各代码段组合起来,以构成一个说明QueryInterface实现及使用的完整例子。总的来说可以将这些代码分成三部分。第一部分是接口IX、IY和IZ的定义部分。接口IUnknown的定义在Win32SDK的头文件1见UNKNWN.H中。第二部分是组件的实现。类CA实现了一个支持IX和IY接口的组件。QueryInterface的实现

    2022年7月22日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号