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. Linux如何统计进程的CPU利用率

    1.0 概述 在Linux的/proc文件系统,可以看到自启动时候开始,所有CPU消耗的时间片:对于个进程,也可以看到进程消耗的时间片.这是一个累计值,可以"非阻塞"的输出.获得一 ...

  2. Delphi word编辑

    private void but_Table_Click(object sender, EventArgs e) { object Nothing = System.Reflection.Missin ...

  3. Twenty Questions

    题意: 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可以问某位上是0还是1. 问最少提问次数,可以把所有字符串区分开来. 分析: dp[s1][s2]: 表示提 ...

  4. 取requests返回字典值用json()

    python模块requests返回值用json()["h"][key]可以取出下面的value

  5. 使用七牛云存储----大家自己的图床[python]

    ##写博客什么的总得贴图吧,图床选来选去还是七牛吧.嗯,就是你了 [OSchaina 源码] 结合FastStone Capture 简直爽歪歪. FastStone Capture 自动保存图片到文 ...

  6. 【JSONCpp】简介及demo

    一.JSON简介 JSON 一种轻量级的数据交换格式,易于阅读.编写.解析,全称为JavsScript ObjectNotation. JSON由两种基本结构组成 ①   名字/值 对的集合,可以理解 ...

  7. Seam carving 学习笔记

    今天首次接触了图像编辑中的seam carving知识,感觉挺神奇的.虽然我自己可能理解的不是很深刻,但是记录下来,总是好的. seam carving直接翻译过来是“线裁剪”的意思.它的主要用途是对 ...

  8. 游戏设计模式:Subclass Sandbox模式,以及功能方法集的设计思考

    书中总结出这种 Subclass Sandbox 的设计模式 Game Design Patterns: Subclass Sandbox 这种模式要点有两点: 在基类中实现各种功能性方法供子类调用 ...

  9. HTML 5:你必须知道的data属性

    原文:All You Need to Know About the HTML5 Data Attribute 译文:你必须知道HTML 5 的Data属性 译者:dwqs HTML 5的Data属性可 ...

  10. ACM2050

    问题描述:   平面上有n条折线,问这些折线最多能将平面分割成多少块? 样例输入 1 2 样例输出 2 7 答案是: 2n ( 2n + 1 ) / 2 + 1 - 2n = 2 n^2 – n + ...