且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将自动完成选择的值绑定到窗体控件名称

更新时间:2023-10-14 14:10:04

确定-找到了解决方案: 问题是没有适当的表单控件绑定到-意味着-数据模型中没有对象(此后通过"form.reset"填充主表单组)同时包含streetId和StreetName.解决方案是扩展数据模型(我创建了一个新类,该类从原始类继承而来,并具有一个更多的selectedStreet属性对象): 这是街道对象:

OK - Found a solution: the problem was that there wasn't an appropriate form control to bind to - meaning - there was no object in the data model (which afterwards was populate the main form group by "form.reset") which contained both the streetId and StreetName. The solution was to extend the data model (I created a new class which inherits from the original one and has one more property object of selectedStreet): This is the street Object:

 public class StreetApi 
{
    public int StreetId { get; set; }
    public int CityId { get; set; }
    public string StreetName { get; set; }
}

这是扩展类:

public class ExtendedUser : User
{...
    public StreetApi SelectedStreet { get; set; }
}

现在-反应形式基于扩展模型,这是自动完成完美工作的方式(不再需要onSelect事件),treesList类型是StreetApi的数组:

Now - the reactive form is based on the extended model, and here is how the auto complete works perfectly (the onSelect event is no longer needed), the streetsList type is an array of StreetApi:

<p-autoComplete formControlName="SelectedStreet" [dropdown]="true" 
 [suggestions]="streetsList" [field] ="'StreetName'" 
 (completeMethod)="searchStreet($event)" [disabled]=!frm.value.CityID 
  ></p- autoComplete>