Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit a @Output event in Angular 2

Angular 2 version used: 2.0.0-alpha.44

I am trying to emit a Output signal from a component. I followed the Angular docs here .Below is the code of my component

@Component({
    selector: 'summary'
})
@View({
    directives: [NgFor, NgIf, ROUTER_DIRECTIVES, LogoutComponent, BarChartDirective, SunburstChartDirective],
    templateUrl: 'view/summary-component.html'
})
export class SummaryComponent {
    @Output() loadSummary: EventEmitter = new EventEmitter();
    project: Project;
    data: any;

    constructor(public http: Http,
            public router: Router,
            public xxxGlobalService: XXXGlobalService) {
        console.log("SummaryComponent: Created");
        this.getSummary()  
    }

    getSummary() {
        this.project = this.xxxGlobalService.getOpenedProject();
        var url = "/project_summary?username="+ this.xxxGlobalService.getUser().name + "&project_id=" + this.project.id;
        this.http.get(url)
                 .map(res => res.json())
                 .subscribe(
                     data => this.extractData(data),
                     err => this.logError(err),
                () => console.log('SummaryComponent: Successfully got the summary')
        );
    }

    extractData(data) {
        this.data = data;
        console.log("SummaryComponent: Emitting loadSummary event");
        this.loadSummary.next("event");
    }
}

However i get error saying "TypeError: router_1.EventEmitter is not a function" when i try to allocate the EventEmitter (below line)

@Output() loadSummary: EventEmitter = new EventEmitter();

I checked the link and modified the line to look like

@Output() loadSummary: EventEmitter;

But the loadSummary seems to be undefined throughout. How to emit the output signal ? Please help

like image 761
Prakash Shanbhag Avatar asked Sep 26 '22 04:09

Prakash Shanbhag


1 Answers

As of beta.0 (and maybe earlier), EventEmitter is now in angular2/core.

Emit an event using emit():

@Output() loadSummary: EventEmitter<any> = new EventEmitter();
...
this.loadSummary.emit("event");
like image 193
Mark Rajcok Avatar answered Oct 06 '22 01:10

Mark Rajcok