Angular2 function call from another component

I have two components: NgbdAlertCloseable and AlertCtrl. I also have the AppComponent component as the parent component. I want to click a button in the AlertCtrl component and create a warning on the NgdbAlertCloseable component.

Function

addSuccess () adds a warning to the view, and it works well when I call it inside my component. However, I tried to use an EventEmitter to call this function from another component (as suggested here: https://stackoverflow.com/a/167954/ ... ), but it gives this error:

ORIGINAL EXCEPTION: TypeError: self._NgbdAlertCloseable_2_4.addSuccess is not a function

Here are my files:

ngbd-watchful-closeable.component.ts

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

@Component({
  selector: 'ngbd-alert-closeable',
  templateUrl: './app/alert-closeable.html'
})
export class NgbdAlertCloseable {

  @Input()
  public alerts: Array<IAlert> = [];

  private backup: Array<IAlert>;

  private index: number; 

  constructor() {
    this.index = 1;
  }

  public closeAlert(alert: IAlert) {
    const index: number = this.alerts.indexOf(alert);
    this.alerts.splice(index, 1);
  }

  public static addSuccess(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'success',
      message: 'This is an success alert',
    });
    this.index += 1;
  }

  public addInfo(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'info',
      message: 'This is an info alert',
    });
    this.index += 1;
  }

}

interface IAlert {
  id: number;
  type: string;
  message: string;
}

watchful-ctrl.component.ts

import { EventEmitter, Output, Component } from '@angular/core';
import { NgbdAlertCloseable } from './ngbd-alert-closeable.component';

@Component({
  selector: 'alert-ctrl',
  template: '<button class="btn btn-success" (click)="addSuccessMsg()">Add</button>'
})

export class AlertCtrl {
    @Output() msgEvent = new EventEmitter(); 
    public addSuccessMsg(){
        this.msgEvent.emit(null);
    }
}

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: '<div class="col-sm-4"><alert-ctrl (msgEvent)="ngbdalertcloseable.addSuccess()"></alert-ctrl><ngbd-alert-closeable #ngbdalertcloseable></ngbd-alert-closeable>'
})

export class AppComponent { }

Am I using this incorrectly? How can i fix this?

+4
1

, addSuccess .

:

 public addSuccess(alert: IAlert) {
    this.alerts.push({
      id: this.index,
      type: 'success',
      message: 'This is an success alert',
    });
    this.index += 1;
 }

IAlert , , msgEvent.emit(IAlert).

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: '<div class="col-sm-4"><alert-ctrl (msgEvent)="ngbdalertcloseable.addSuccess($event)"></alert-ctrl><ngbd-alert-closeable #ngbdalertcloseable></ngbd-alert-closeable>'
})

export class AppComponent { }

IAlert, AlertCtrl .

import { EventEmitter, Output, Component } from '@angular/core';
import { NgbdAlertCloseable } from './ngbd-alert-closeable.component';

@Component({
  selector: 'alert-ctrl',
  template: '<button class="btn btn-success" (click)="addSuccessMsg()">Add</button>'
})

export class AlertCtrl {

    currentAlert:IAlert = {id: 0, type: 'success', message: 'This is an success alert'};

    @Output() msgEvent = new EventEmitter<IAlert>(); 
    public addSuccessMsg(){
        this.msgEvent.emit(this.currentAlert);
    }
}

!

+1

Source: https://habr.com/ru/post/1653297/


All Articles