[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 value below and title color will also change.
color-picker.ts:
import {Component, Output, EventEmitter, Input} from "@angular/core";
import {RED, BLUE} from './constants';
@Component({
selector: 'color-picker',
moduleId: module.id,
templateUrl: 'color-picker.component.html'
})
export class ColorPicker{
@Input() color:string;
@Output('selectedColor') colorOut = new EventEmitter();
red = RED;
blue = BLUE;
choose(color){
this.colorOut.emit(color);
}
}
color-picker.component.html:
<div class="color-title" [ngStyle]="{color:color}">Pick a Color, plz:</div>
<div class="color-picker">
<div class="color-sample color-sample-blue" (click)="choose(red)"></div>
<div class="color-sample color-sample-red" (click)="choose(blue)"></div>
</div>
So the logic is we will take a color input, it is used in title styling:
<div class="color-title" [ngStyle]="{color:color}">Pick a Color, plz:</div>
When we click on one rect box, fire choose() function, it will output a event named "selectedColor":
@Output('selectedColor') colorOut = new EventEmitter();
choose(color){
this.colorOut.emit(color);
}
If we don't give name 'selectorColor', it will used 'colorOut' as name.
In app.ts, it is used:
<color-picker #picker [color]="picker.color" (selectedColor)="picker.color = $event">
</color-picker>
{{picker.color}}
Here we use reference:
#picker
And we assign the color back from output event to picker.color, this picker.color then will be used as input to color-pick to change the title color.
The benfits to use reference is avoid assign a local variable.
[Angular 2] @Input & @Output Event with ref的更多相关文章
- Angular 个人深究(三)【由Input&Output引起的】
Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...
- [Angular] Testing @Input and @Output bindings
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- angular 的 @Input、@Output 的一个用法
angular 使用 @input.@Output 来进行父子组件之间数据的传递. 如下: 父元素: <child-root parent_value="this is parent ...
- Angular中input和output使用
// 写法一: 1 @Components({ 2 ...., 3 inputs:['init'], 4 outputs:['finish'] 5 }) 6 export class xxx(){ 7 ...
- PHP-FPM-failed to ptrace(PEEKDATA) pid 123: Input/output error
If you're running PHP-FPM you can see these kind of errors in your PHP-FPM logs. $ tail -f php-fpm.l ...
- NFS挂载异常 mount.nfs: Input/output error
[root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...
- BIOS(Basic Input/Output System)是基本输入输出系统的简称
BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...
- read()、write()返回 Input/output error, Device or resource busy解决
遇到的问题,通过I2C总线读.写(read.write)fs8816加密芯片,报错如下: read str failed,error= Input/output error! write str fa ...
- Docker 在转发端口时的这个错误Error starting userland proxy: mkdir /port/tcp:0.0.0.0:3306:tcp:172.17.0.2:3306: input/output error.
from:https://www.v2ex.com/amp/t/463719 系统环境是 Windows 10 Pro,Docker 版本 18.03.1-ce,电脑开机之后第一次运行 docker ...
随机推荐
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- cocos2d-x 详解之 CCSprite(精灵)- “CCSpriteBatchNode”和“CCSpriteFrameCache”
帧动画-手动切换帧-批次渲染处理动画-纹理图片的本质 ------------------------------------------------------------------------- ...
- linux下配置双网卡及RAC规划——1
使用背景: 操作系统:centos 虚拟机:virtualbox RAC系统中需要双网卡,一个为公共的网络环境,一个为私有的网络环境,从而需要搭建双网络. 在菜单FILE中选择preferences, ...
- Javascript——说说js的调试
最近比较吐槽,大家都知道,现在web前端相对几年前来说已经变得很重了,各种js框架,各种面对对象,而且项目多了,就会提取公共模块. 这些模块的UI展示都一样,不一样的就是后台逻辑,举个例子吧,我们做企 ...
- MVC3.0在各个版本IIS中的部署
概述: 最近在做一个MVC 3的项目,在部署服务器时破费了一番功夫,特将过程整理下来,希望可以帮到大家! 本文主要介绍在IIS5.1.IIS6.0.IIS7.5中安装配置MVC 3的具体办法! 正文: ...
- 叉积判断 POJ1696
// 叉积判断 POJ1696 #include <iostream> #include <algorithm> #include <cstring> #inclu ...
- Spark中的编程模型
1. Spark中的基本概念 Application:基于Spark的用户程序,包含了一个driver program和集群中多个executor. Driver Program:运行Applicat ...
- ehcache 的配置
配置:一.在src目录下加入ehcache.xml: <cache name="SimplePageCachingFilter" maxElementsInMemory=&q ...
- tomcat http https
Tomcat如何既支持http又支持https?在server.xml中开启两个connector: http: <Connector port="8080" maxHttp ...
- Spark RDD概念学习系列之Spark的数据存储(十二)
Spark数据存储的核心是弹性分布式数据集(RDD). RDD可以被抽象地理解为一个大的数组(Array),但是这个数组是分布在集群上的. 逻辑上RDD的每个分区叫一个Partition. 在Spar ...