[Angular 2] @Input & @Output Event with ref

Source: Internet
Author: User
Tags emit export class

The application is simple and to build a color picker:

When click the Rect box, it'll check the color value below and title color would also change.

Color-picker.ts:

Import {Component, Output, Eventemitter, Input} from "@angular/core"'./constants '; @Component ({    ' Color-picker ',    moduleId:module.id,    ' color-picker.component.html ') export class colorpicker{    @Input () color:string;    @Output (new  eventemitter ();     = RED;     = BLUE;    Choose (color) {        this. Colorout.emit (color);    }}

Color-picker.component.html:

<Divclass= "Color-title"[Ngstyle]= "{Color:color}">Pick a Color, plz:</Div><Divclass= "Color-picker">    <Divclass= "Color-sample color-sample-blue"(click)= "Choose (red)"></Div>    <Divclass= "Color-sample color-sample-red"(click)= "Choose (blue)"></Div></Div>

So the logic is we'll take a color input, it's used in title styling:

<class= "Color-title"  [Ngstyle]= "{Color:color}"> Pick a Color, plz:</div>

When we click on the One Rect box, fire choose () function, it'll output a event named "Selectedcolor":

New Eventemitter ();    Choose (color) {        this. Colorout.emit (color);    }

If we don ' t give name 'selectorcolor', it'll 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'll be used as I Nput 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.