Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: ctx.onCreditChange is not a function angular

I have two components. Component A which is the parent and component B which is the child. Component A looks like this:

A.html

    <nb-box [onCreditChange]="onCreditChange"></nb-box>

A.ts

onCreditChange($event) { console.log($event) }

The function from component A is transferred to B.

Component B looks like this

B.html

<div class="box">
 <nb-switch  (onChange)="onCreditChange($event)"></nb-switch>

</div>

B.ts (part of this component)

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

export class Box extends BoxBase {
  @Output() onCreditChange: EventEmitter<any> = new EventEmitter()

}

I get an error when calling the function

core.js:6241 ERROR TypeError: ctx.onChange is not a function

Do you know how to fix it?

like image 587
Doe Avatar asked Mar 01 '26 06:03

Doe


1 Answers

PARENT COMPONENT

HTML

<nb-box (onCreditChange)="onCreditChange"></nb-box>

TS

onCreditChange($event) { console.log($event) }

CHILD COMPONENT

The error start here, because Output is not a function, it's an object that allow to you to send events to the parent. You need to do a function in child an inside of that function emit with the output object.

HTML

<div class="box">
 <nb-switch  (onChange)="onChangeInChild($event)"></nb-switch>

</div>

TS

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

export class Box extends BoxBase {
  @Output() onCreditChange = new EventEmitter<any>()

  onChangeInChild(eventObject: any){
      this.onCreditChange.emit(eventObject)
  }
}
like image 155
Derek Menénedez Avatar answered Mar 02 '26 18:03

Derek Menénedez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!