angular - 布尔值在客户端上以角度返回未定义
问题描述
我有一个 asp.net 核心后端,通过 api 调用获取数据。我有这个对象模型:
public class CostGroup
{
public long Id { get; set; }
public string Name { get; set; }
public int Rank { get; set; }
public bool HcPlanning { get; set; }
public bool OtherPlantCost { get; set; }
public bool DirectLabCost { get; set; }
public long CompanyId { get; set; }
public Company Company { get; set; }
}
这个api调用:
[HttpGet("{id}")]
public CostGroup GetCostGroup(long Id)
{
return DataContext.CostGroups.Find(Id);
}
这是angular中的相应服务:
getCostGroup(id: number) {
return this.http.get<CostGroup>('api/masterdata/costaccounting/costgroups/' + id);
}
一切正常,我在客户端得到这样的结果:
{"id":24,"name":"ss","rank":123,"hcPlanning":true,"otherPlantCost":true,"directLabCost":true,"companyId":1,"company":null}
如您所见,布尔值被正确识别。但是如果我想读取布尔值,我会得到未定义的。CostGroup
我像这样以角度读取对象:
this.service.getCostGroup(e.key).subscribe(costGroup => {
this.costGroup = costGroup;
this.companyId = costGroup.companyId;
this.hcplanning = costGroup.hcplanning;
this.otherplantcost = costGroup.otherplantcost;
this.directlabcost = costGroup.directlabcost;
alert(costGroup.directlabcost);
this.editMode = true;
this.name = costGroup.name;
this.popup.instance.show();
});
所有布尔字段都是未定义的。为什么?如何克服这个问题?
解决方案
您的对象属性为驼峰式:
"otherPlantCost":true
但是,您以小写形式访问它们:
this.otherplantcost = costGroup.otherplantcost;
您必须在骆驼情况下正确访问它们:
this.otherplantcost = costGroup.otherPlantCost;
推荐阅读
- python - IndexError:即使我没有引用大于列表长度的值,列表索引也超出范围?
- java - Nutch 爬行给出错误“来自 http://localhost:8983/solr/nutch 的服务器错误:java.lang.NullPointerException”
- spring - Spring 集成测试 Oracle Native SQL
- visual-studio - Xamarin iOS 图形用户界面
- typescript - 为数组数据类型创建接口
- javascript - 电话号码的Angular 7输入掩码
- swift - StackView 内的 UILabel 的 UITapGestureRecognizer
- android - 如何检测android中的滚动视图是否可滚动
- python-3.x - 一个单一的 Tkinter 窗口,它在帧中移动,将每一帧带到前台。一帧需要分成两个子帧
- 3d - 对象模式和编辑模式之间的根本区别是什么?