【发布时间】:2019-08-16 15:28:13
【问题描述】:
我正在尝试从 SQL 服务器数据库中获取数据并将响应放入前端的字段中。我正在获取数据,但响应中的某些字段不是唯一的,并且在下拉列表中显示重复的值。我的组件如下所示
export class ReportingFilterComponent implements OnInit {
ShipmentList: ShipmentByProject[];
output= [];
entityUrl = 'ShipmentDetail/GetByReportingProject?repPrj=000634';
constructor(service: DataService) {
this.ShipmentList = [];
service.get<ShipmentByProject[]>(this.entityUrl).subscribe(x => {this.ShipmentList = x });
}
ngOnInit() {
var flags= [];
var l = Array.isArray(this.ShipmentList) ? this.ShipmentList.length : 0, i;
for( i=0; i<l; i++) {
if( flags[this.ShipmentList[i].customer_shipto_name]) continue;
flags[this.ShipmentList[i].customer_shipto_name] = true;
this.output.push(this.ShipmentList[i].customer_shipto_name);
}}
为了测试我在 html 中使用了 output 数组
Output data!
<li *ngFor="let out of output">
{{ out.customer_shipto_name }}
</li>
<div class="dx-fieldset">
<div class="dx-field">
<div class="dx-field-label">ShipTo Account</div>
<div class="dx-field-value">
<dx-select-box [dataSource]="ShipmentList" displayExpr="customer_shipto_name"></dx-select-box>
</div>
</div>
【问题讨论】:
-
假设您的
service.get<ShipmentByProject>[]>调用返回了一个承诺,那么在承诺解决之前,this.ShipmentList似乎是未定义的。您可以将值初始化为空数组以消除错误,或者在尝试读取长度之前检查它是否存在。 -
@JoshRutherford 我该怎么做,抱歉第一次在 Angular 编程时生硬
-
没问题,看贴出来的答案
-
@JoshRutherford 我已经从模型类
ShipmentByProject初始化了 ShipmentList -
你在这里所做的是指定
ShipmentList的类型(使用冒号语法),但你没有给它一个值。那看起来像ShipmentList: ShipmentByProject[] = []
标签: javascript angular typescript angular7