10
import { Component, Input, Output, EventEmitter } from '@angular/core';

var colorPickerCss = "app/css/ui/color-picker.css";
var colorPickerTemplate = "app/partials/color-picker.html";

@Component({
    selector: 'color-picker',
    styleUrls: [colorPickerCss],
    templateUrl: colorPickerTemplate

})
export class ColorPicker{
    @Input() colors: string[] = [];
    @Output() selectedColor = new EventEmitter();
    isSelectorVisible : boolean = false;

    showSelector(value: boolean){
        this.isSelectorVisible = value;
    }
    selectColor(color: string){
        this.showSelector(false);
        this.selectedColor.next({color});
    }


} ;

I have written the above code, but I want to understand the functioning of it. My question is, what is the .next() function on this line this.selectedColor.next({color}). What library is it from? I have mentioned the imports above, but I can't really get to the actual definition of this function.

alexmac
  • 16,798
  • 7
  • 48
  • 61
  • 2
    next is deprecated - see [this question](https://stackoverflow.com/questions/35840576/difference-between-eventemitter-next-and-eventemitter-emit-in-angular-2) and the answer there also mentions [this resource](https://github.com/angular/angular/blob/b5b6ece65a96f5b8f134ad4899b56bf84afe3ba0/modules/angular2/src/facade/async.dart#L49) if that helps in your understanding – 0mpurdy Jul 29 '17 at 20:31

1 Answers1

5

An EventEmitter, extends Subject. When you use next, you fire off an event that all subscribers will listen too. See here. emit is the preferred alternative.

JGFMK
  • 7,107
  • 4
  • 46
  • 80
  • Correct me if I'm wrong - a first subscriber would be the view that may change the color based on this variable update. Is this true ? eg. if I have in the view something like This text is colored with the selected color the text will be displayed with different colors as the variable is changing. Other way asked - are the variables in view automatically subscribers in the sense you said above? – Victor Jan 06 '21 at 15:11
  • I am sorry. It's been 3.5 years since I wrote this. I've been working with a lot of other techs since then. May be work fielding this as a separate question instead of a comment. – JGFMK Jan 06 '21 at 15:55