Javascript 返回未定义的角度分量变量
我使用的是Angular 10,我在使用ControlValueAccessor创建输入组件时遇到了一些问题 我正在创建公共变量和公共arrow函数,当我调用arrow函数时,返回undefined 这是我的.ts代码:Javascript 返回未定义的角度分量变量,javascript,angular,variables,Javascript,Angular,Variables,我使用的是Angular 10,我在使用ControlValueAccessor创建输入组件时遇到了一些问题 我正在创建公共变量和公共arrow函数,当我调用arrow函数时,返回undefined 这是我的.ts代码: import { Component, forwardRef, Input } from '@angular/core'; import { ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR } fr
import { Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'app-input',
templateUrl: './input.component.html',
styleUrls: ['./input.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(
() => InputComponent
),
multi: true
}
]
})
export class InputComponent implements ControlValueAccessor {
@Input() public parentForm: FormGroup;
@Input() public fieldName: string;
@Input() public label: string;
public value: string;
public changed: ( value: string ) => void;
public touched: () => void;
public isDisabled: boolean;
get formField (): FormControl {
return this.parentForm?.get( this.fieldName ) as FormControl;
}
constructor () { }
public writeValue ( value: string ): void {
this.value = value;
}
public onChange ( event: Event ): void {
const value: string = (<HTMLInputElement>event.target).value;
this.changed( value );
}
public registerOnChange ( fn: any ): void {
this.changed = fn;
}
public registerOnTouched ( fn: any ): void {
this.touched = fn;
}
public setDisabledState ( isDisabled: boolean ): void {
this.isDisabled = isDisabled;
}
}
我相信this.change错误是因为我调用了onChange函数,而ctx.touch是因为我调用了HTML文件
我可以正常访问Input()变量,如parentForm、fieldName和label
谢谢您的帮助。更改这些行
public changed: ( value: string ) => void;
public touched: () => void;
到
我已更改,并且没有收到任何错误,但我的值没有更改,在FormGroup上仍然为null。你也能帮我吗?恐怕我得走了,但我的一辆旧汽车上有stackblitz。通过
[formControl]=“myControlName”
进行绑定将在内部调用寄存器函数,一旦更改
将更新父窗体值。如果你更新了有人可以帮助的问题,我可能会在某个时候使用formControl返回到这个问题,现在正在工作,非常感谢!
ERROR TypeError: this.changed is not a function
ERROR TypeError: ctx.touched is not a function
public changed: ( value: string ) => void;
public touched: () => void;
public changed: any = ( value: string ) => void; // personally I prefer {} rather than void
public touched: any = () => void;