简体   繁体   English

Angular - 记录 eventemitter.emit 日志“未定义”

[英]Angular - Logging eventemitter.emit logs 'undefined'

I'm trying to emit a number value from a component.我正在尝试从组件中发出一个数值。 I'm logging the value before it's emitted, and it has the correct value.我在它发出之前记录值,它有正确的值。

The parent component where im refering to my hours and minutes component.我指的是我的小时和分钟组件的父组件。

<app-hours-and-minutes-selector [(hours)]="model.overTimeHours"
                                            [(minutes)]="model.overTimeMinutes"></app-hours-and-minutes-selector>

model.overTimeHours and model.overTimeMinutes is numbers model.overTimeHours 和 model.overTimeMinutes 是数字

hours and minutes component小时和分钟组件

<div class="hours-and-minutes-inputs">
    <input class="hours-input header-1"
           inputmode="numeric"
           [appRegexMask]="hoursRegEx"
           placeholder="0"
           (blur)="hoursInputBlur($event.target.value)"
           [value]="hours">
    <span class="big-copy">{{hours | plural : ('time.hour' | translate) : ('time.hours' |
        translate)}}</span>
    <input class="minutes-input header-1"
           inputmode="numeric"
           [appRegexMask]="minutesRegEx"
           placeholder="0"
           (blur)="minutesInputBlur($event.target.value)"
           [value]="minutes">
    <span class="big-copy">{{'shared.minutes-short' | translate}}</span>
</div>
import { Component, EventEmitter, Input, Output } from '@angular/core';

@Component({
    selector: 'app-hours-and-minutes-selector',
    templateUrl: './hours-and-minutes-selector.component.html',
    styleUrls: ['./hours-and-minutes-selector.component.scss']
})
export class HoursAndMinutesSelectorComponent {

    public hoursRegEx = '^[0-9]{0,3}$';
    public minutesRegEx = '^([0-9]|[0-5][0-9])$';
    @Input() public hours: number;
    @Input() public minutes: number;
    @Output() public hoursTyped: EventEmitter<number>;
    @Output() public minutesTyped: EventEmitter<number>;

    constructor() {
        this.hoursTyped = new EventEmitter();
        this.minutesTyped = new EventEmitter();
    }

    public hoursInputBlur(hours: number): void {
        this.hours = hours;
        this.hoursTyped.emit(this.hours);
        // logs undefined
        console.log(this.hoursTyped.emit(hours));

    }
    public minutesInputBlur(minutes: number): void {
        this.minutes = minutes;
        this.minutesTyped.emit(this.minutes);

    }

}

I've tried to move the initialazation to where i declare the eventemitter without any difference.我试图将初始化移动到我声明 eventemitter 的地方,没有任何区别。

Can someone please tell me where i'm wrong?有人可以告诉我哪里错了吗?

For sharing data between parent and child component, we only require square brackets.为了在父子组件之间共享数据,我们只需要方括号。 So it should be:所以应该是:

<app-hours-and-minutes-selector [hours]="model.overTimeHours" [minutes]="model.overTimeMinutes"></app-hours-and-minutes-selector>

if you want to receive a value from your event emitter you have to use event binding (use only parenthesis) because your emitter is an event on your HoursAndMinutesSelectorComponent and not a number, also on the Parent component you have to define a function that will receive a value from the emitter and set in your variable and finally the event that will emit the component should be called equal your emitter, example:如果你想从你的事件发射器接收一个值你必须使用事件绑定(只使用括号)因为你的发射器是你的 HoursAndMinutesSelectorComponent 上的一个事件而不是一个数字,同样在父组件上你必须定义一个 function 将接收来自发射器的一个值并设置在你的变量中,最后将发射组件的事件应该被称为等于你的发射器,例如:

Html: Html:

<app-hours-and-minutes-selector (hoursTyped)="onAsignHours($event)"
                                                (minutesTyped)="onAsignNumber($event)"></app-hours-and-minutes-selector>

TS: TS:

onAsignHours(hours: number){this.model.overTimeHours = hours}

Note that I only use parenthesis and the event name is the same that the emitter (where the value comes), and I define a new function that will call when a new value is emitted and will be responsible to assign the value that comes from emitter to a variable.请注意,我只使用括号并且事件名称与发射器(值来自的地方)相同,并且我定义了一个新的 function,它将在发出新值时调用并将负责分配来自发射器的值到一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM