You are asked to create a simple Angular component named CounterComponent having counter-component for selector. The purpose of this component is to: Take a message in "input" Read an interval value entered by the user (integer, in milliseconds) Send the message as events periodically to its "output" At the HTML level, the component is composed of an field with id="intervalInput" and a button with id="intervalSetButton" . The user can change the interval by entering a value in the field then clicking on the button.
The component must have an @Input named message and an @Output named tick . It should generate "string" type events to tick at regular intervals corresponding to the value entered by the user. The string used to generate the events comes from the "input" message. The component must be able to handle message changes and value changes of the interval. Notes: You must modify the template as well as the class of the component. A block of "Preview" is available to allow you to debug your code. It displays your component below. You can open it and modify it as you wish. To help you, this block contains a parent component that manipulates the component you need to create (displaying a counter next to the message) This block of "Preview" is not taken into account in the calculation of the score of your code
import { Component, Output, Input, EventEmitter, OnDestroy, } from '@angular/core';
@Component({
selector: 'counter-component',
template: `
<input id="intervalInput" type="number" [value]="inputValue" (input)="setValue($event)"/>
<button id="intervalSetButton" (click)="setPeriod()">Set interval</button>
`,
})
export class CounterComponent implements OnDestroy {
public inputValue: string = '';
public thePeriod: any; //NodeJS.Timer;
@Input() message!: string;
@Output() public tick: EventEmitter<string> = new EventEmitter();
public setValue(e: Event) {
const target = e.target as HTMLTextAreaElement;
this.inputValue = target.value;
}
public setPeriod(): void {
this.thePeriod = setInterval(() => this.tick.emit(this.message), +this.inputValue);
}
ngOnDestroy(): void {
clearInterval(this.thePeriod);
}
}
- Edité par AbdeAMNR 27 décembre 2022 à 1:42:25
angular counter component
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.