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的更多相关文章

  1. Angular 个人深究(三)【由Input&Output引起的】

    Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...

  2. [Angular] Testing @Input and @Output bindings

    Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...

  3. angular 的 @Input、@Output 的一个用法

    angular 使用 @input.@Output 来进行父子组件之间数据的传递. 如下: 父元素: <child-root parent_value="this is parent ...

  4. Angular中input和output使用

    // 写法一: 1 @Components({ 2 ...., 3 inputs:['init'], 4 outputs:['finish'] 5 }) 6 export class xxx(){ 7 ...

  5. 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 ...

  6. 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 ...

  7. BIOS(Basic Input/Output System)是基本输入输出系统的简称

    BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...

  8. read()、write()返回 Input/output error, Device or resource busy解决

    遇到的问题,通过I2C总线读.写(read.write)fs8816加密芯片,报错如下: read str failed,error= Input/output error! write str fa ...

  9. 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 ...

随机推荐

  1. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. 解决32位plsql客户端连接不64位Oracle11g上数据库

    一.解决方案 因为本人安装的是64位的Oracle,plsql 是32位的故连接不上.网上有方法能连接. 1. 文件下载 下载PLSQL_Developer地址 http://pan.baidu.co ...

  3. C# 一次查询多表,填充DataSet并指定表名

    lhrhi 原文 NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧) 现实中的场景,有时可能需要一次查询数据库中表张.在使用SqlDataAdapte ...

  4. C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)

    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...

  5. ubuntu下安装selenium2.0 环境

    参考:http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html ubuntu 安装过程: 1.安装:setuptools $ apt-ge ...

  6. 用duilib制作仿QQ2013动态背景登录器

    转载请说明原出处,谢谢~~ 在上一篇博客里,我修复了CActiveXUI控件的bug,从而可以使用flash动画来制作程序的背景,这篇博客说明一下应该怎么使用CActiveXUI控件创建透明无窗体的背 ...

  7. 关于jQuery中,animate、slide、fade等动画的连续触发、滞后反复执行的bug的个人解决办法

    照例,现在开头讲个这个问题发生的背景吧: 因为最近要做个操作选项的呼出,然后就想到了用默认隐藏,鼠标划过的时候显示的方法. 刚开始打算添加一个class="active",直接触发 ...

  8. Android相关图书推荐

    疯狂Android讲义(第3版 附光盘) 作      者 李刚 著 出 版 社 电子工业出版社 出版时间 2015-06-01 版      次 3 页      数 780 印刷时间 2015-0 ...

  9. oc_转_构造对象的方法,以及类的继承

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情: 1) 使用alloc方法来分配存储空间 ...

  10. hdfs[命令] dfs

    Usage: hadoop fs [generic options] [-appendToFile <localsrc> ... <dst>] [-cat [-ignoreCr ...