gpt4 book ai didi

angular - 如何获取绑定(bind) [(ngModel)] 的属性名称? Angular 6

转载 作者:太空狗 更新时间:2023-10-29 18:00:42 26 4
gpt4 key购买 nike

我有以下自定义组件

<form-text
[(ngModel)]="dataItem.prop1">
</form-text>

在这种情况下,如何从类“prop1”中获取属性名称?

  @Component({
selector: 'form-text',
template: `
<div>
<label *ngIf="label" [attr.for]="identifier">{{label}}</label>
<input
type="text"
[(ngModel)]="value"
/>
</div>
`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: FormTextComponent,
multi: true,
}],
})
export class FormTextComponent extends ElementBase<string>{

@ViewChild(NgModel) model: NgModel;

constructor(@Optional() @Inject(NG_VALIDATORS) validators: Array<any>){
super(validators);
}

}

angular 以某种方式知道它应该更新哪个属性。我想获取此属性的引用或名称。谢谢

最佳答案

You can pass an @input property 'propname' with value as string.

<form-text
required
hexadecimal
[dataV]="data"
placeholder="Enter a hexadecimal value"
label="Value 1"
#prop1=ngModel
[(ngModel)]="dataItem.prop1"
propname="'prop1'"
>
</form-text>

In FormTextComponent,

@input() propname: string;
@ViewChild('prop1') propmodel: ngModel;

console.log(this.propname); // prop1
console.log(this.propmodel);

关于angular - 如何获取绑定(bind) [(ngModel)] 的属性名称? Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com