简体   繁体   English

布尔值在客户端上以角度返回未定义

[英]boolean value returns undefined on the client in angular

I have an asp.net core backend, where I get the data via api calls.我有一个 asp.net 核心后端,在那里我通过 api 调用获取数据。 I have this object model:我有这个对象模型:

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; }
}

And this api call:这个api调用:

[HttpGet("{id}")]
public CostGroup GetCostGroup(long Id)
{
    return DataContext.CostGroups.Find(Id);
}

And this is the corresponding service in angular:这是 angular 中的相应服务:

  getCostGroup(id: number) {
    return this.http.get<CostGroup>('api/masterdata/costaccounting/costgroups/' + id);
  }

Everything works, I get the result on the client side like this:一切正常,我在客户端得到这样的结果:

{"id":24,"name":"ss","rank":123,"hcPlanning":true,"otherPlantCost":true,"directLabCost":true,"companyId":1,"company":null}

As you see, the boolean values are correct recognized.如您所见,布尔值被正确识别。 But if I want to read the boolean values, I get undefined.但是如果我想读取布尔值,我会得到未定义。 I read the CostGroup object in angular like this:我以CostGroup的角度读取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();
});

All boolean fields are undefined.所有布尔字段都是未定义的。 Why?为什么? How to overcome this issue?如何克服这个问题?

Your object properties are in camel case:您的对象属性是驼峰式的:

"otherPlantCost":true

However you are accessing them in lower case:但是,您以小写形式访问它们:

this.otherplantcost = costGroup.otherplantcost;

You must access them correctly in camel case:您必须在骆驼情况下正确访问它们:

this.otherplantcost = costGroup.otherPlantCost;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM