Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
666 B
2
Indexable
Never
import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <button (click)="changeTest()">Change Test</button>
  `,
})
export class ChildComponent {
  @Input() test!: number;
  @Output() testChange = new EventEmitter<number>();

  changeTest() {
    this.test += 10;
    this.testChange.emit(this.test);
  }
}

@Component({
  selector: 'app-parent',
  template: `
    <p>Parent Test: {{ test }}</p>
    <app-child [test]="test" (testChange)="onTestChange($event)"></app-child>
  `,
})
export class ParentComponent {
  test = 5;

  onTestChange(newValue: number) {
    this.test = newValue;
  }
}