博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular4父组件向子组件传值,子组件向父组件传值的方法
阅读量:6871 次
发布时间:2019-06-26

本文共 1649 字,大约阅读时间需要 5 分钟。

交流知识获取视频资料:

父组件向子组件传值   @Input

文件目录

 

父组件:

father.template.html

父组件

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
(); id:any ="我是传给父组件的值" ngOnInit() { } ngOnChanges() { } checkedCallback() { this.checkedBack.emit(this.id); }}

child.template.html.html

子组件

父组件

father.template.html

父组件

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://www.cnblogs.com/wangzhichao/p/9817312.html

你可能感兴趣的文章
oracle 11g R2 64位 安装详细步骤
查看>>
Jpeg 库的解码OpenCL优化
查看>>
正则表达式
查看>>
『中级篇』docker之虚拟机创建vagrant技巧(番外篇)(81)
查看>>
交换机SPAN功能配置
查看>>
MySQL 架构组成—存储引擎
查看>>
基于数值分析思想对多项式求值的原理和应用进行探究
查看>>
vue-devtools vue开发调试神器
查看>>
PHP扩展模块的安装
查看>>
BGP基础操作
查看>>
SimpleXml项目
查看>>
php下使用PDO创建sqlite3数据库
查看>>
Istio技术与实践6:Istio如何为服务提供安全防护能力
查看>>
ISTP的重要作用
查看>>
驼峰设计 PPT美化
查看>>
Python Python 正则 取中括号值
查看>>
uva 658(Dijkstra)
查看>>
uva 11183(最小树形图)
查看>>
sql 集合查询 数据更新操作语句
查看>>
静态内部类
查看>>