angular 的 @Input、@Output 的一个用法
angular 使用 @input、@Output 来进行父子组件之间数据的传递。
如下:
父元素:
<child-root parent_value="this is parent value" (child_emit)="test()"></child-root>
父元素标签中有一个属性是,parent_value,在子元素中可以使用该值:
<p [title]="parent_value" >this paragraph's title is from parent's(parent-root) attribute parent_value</p>
在子元素中,我们 p 标签的 title 属性绑定了父元素的 parent_value 属性,这里要注意了,
[title]="parent_value" 的意思并不是指 title 的值就是 "parent_value" 这个字符串,而是父元素中指定的 parent_value 属性的值。
这里,我们需要做的处理是,在子组件中,使用 @Input 去注解 parent_value 属性,指明这是一个来自父组件的元素。
在上面的例子中,父元素也定义了一个属性 child_emit,值是 test(),也就是说这是一个函数调用,在父元素组件中有一个 test 函数,可是我们应该怎么调用呢?我们毕竟没有 child_emit 这种事件,这时候我们就可以在子元素中触发父元素的这个 test 方法的调用。
但是首先我们先要在子元素组件中把 child_emit 使用 @Output 进行注解,然后将其值设为 new EventEmitter,当我们在子组件中去调用 child_emit 方法的时候,父元素中 child_emit 的值的方法(test)就会被调用。
源码如下:
child.component.ts
import {Component, EventEmitter, Input, Output} from "@angular/core";
@Component({
selector: 'child-root',
template: `
<p [title]="parent_value" >this paragraph's title is from parent's(parent-root) attribute parent_value</p>
<button class="btn-font-family" (click)="trigger()">点击的时候会触发父元素 example-root 的 child_emit 对应的事件</button>
`
})
export class ChildComponent {
@Input()
parent_value;
@Output()
child_emit = new EventEmitter();
trigger() {
this.child_emit.emit()
}
}
parent.component.ts
import {Component, Output} from "@angular/core";
@Component({
selector: 'example-root',
template: `
<child-root [parent_value]="parent_value" (child_emit)="test()"></child-root>
`,
})
export class ParentComponent {
@Output()
parent_value = 'value from parent';
test () {
console.log('call by emit at ' + new Date().toLocaleString())
}
}
完整源码:https://github.com/eleven26/angular-example/tree/master/src/input_output_example
angular 的 @Input、@Output 的一个用法的更多相关文章
- [Angular 2] @Input & @Output Event with ref
The application is simple, to build a color picker: When click the rect box, it will check the color ...
- Angular 个人深究(三)【由Input&Output引起的】
Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...
- java—数组乘积输入: 一个长度为n的整数数组input 输出: 一个长度为n的数组result,满足result[i] = input数组中,除了input[i] 之外的所有数的乘积,不用考虑溢出例如 input {2, 3, 4, 5} output: {60, 40, 30, 24}
/** * 小米关于小米笔试题 数组乘积输入: 一个长度为n的整数数组input 输出: 一个长度为n的数组result,满足result[i] = * input数组中,除了input[i] 之外的 ...
- [Angular] Testing @Input and @Output bindings
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- Angular中input和output使用
// 写法一: 1 @Components({ 2 ...., 3 inputs:['init'], 4 outputs:['finish'] 5 }) 6 export class xxx(){ 7 ...
- BIOS(Basic Input/Output System)是基本输入输出系统的简称
BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...
- poj 1182 食物链 并查集的又一个用法
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41584 Accepted: 12090 Descripti ...
- cannot access Input/output error
ls: cannot access Input/output errorls: cannot open directory .: Input/output error 硬盘故障,只读或只写,你可以d ...
- CentOS 启动提示unexpected inconsistency;RUN fsck MANUALLY, ntfs的input/output Error,InPageError c000009c使用chkdsk修复磁盘,12款Linux系统恢复工具
CentOS这两天服务器出了问题了,提示如下: unexpected inconsistency;RUN fsck MANUALLY An error occurred during the file ...
随机推荐
- Python3实现机器学习经典算法(二)KNN实现简单OCR
一.前言 1.ocr概述 OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然 ...
- Java:有关自定数组的学习
Java:有关==自定数组==的学习 在 ==<Java程序设计与数据结构教程>== 里我在==P212~P213==页看到一个GradeRange的程序,它用的数组是自定设定的Grade ...
- asp.netcore mvc 权限拦截
1-背景介绍 需要做一个简单权限系统,基于 角色,用户,菜单 的模式 基于IActionFilter全局拦截,在内部跳转或者浏览器跳转的时候,拦截是成功的,当通过AJAX 请求的时候,页面就不会跳转 ...
- (工具类)MD5算法|时间格式转换|字符串转数字
package vote.utils; import java.security.MessageDigest; import java.text.SimpleDateFormat; import ja ...
- HDU 4489 The King’s Ups and Downs dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 The King's Ups and Downs Time Limit: 2000/1000 ...
- lintcode-511-交换链表当中两个节点
511-交换链表当中两个节点 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个 ...
- 1014 我的C语言文法定义与C程序推导过程
程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> < ...
- 201621123037 《Java程序设计》第8周学习总结
作业08-集合 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 答: 思维导图: 其他-笔记: 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayLi ...
- CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)
http://codeforces.com/contest/1092/problem/D2 Vova's family is building the Great Vova Wall (named b ...
- testng几种写法
testng几种写法: 1 <!--运行-类--> 2 <?xml version="1.0" encoding="UTF-8"?> 3 ...