在typescript中调用类的特性而不定义模型类(typescript)

时间:2018-04-11 18:06:24

标签: .net angular typescript core

我正在研究带有.net核心的角度4。我使用Web API将数据从服务器(oracle)传递到客户端。有没有办法在不创建模型类的情况下传递数据(使用get / post或任何其他方式)?为我们的实体定义结构的问题是我们有大量数据(许多表有很多列)并且手动定义所有实体将非常耗时。 数据流有点像这样:       Model.cs-> Controller.cs-> Service.ts-> Model.ts-> Component.ts-> component.html

*想要摆脱model.ts。

2 个答案:

答案 0 :(得分:0)

您不必声明类型。您可以在component.ts中订阅这样的api响应:

myGetSomeDataResponse: any;
objectKeys = [];
...
this.myService.getSomeData().subscribe(response => {
   this.myGetSomeDataResponse = response;
   this.objectKeys = Object.keys(this.myGetSomeDataResponse);
});

在您的组件的HTML中:

<p *ngFor="let property of objectKeys">{{myGetSomeDataResponse[property]}}</p>

答案 1 :(得分:0)

May be, I was not clear with my question. What if I don't know the properties names of my entity? Is there any way to access these properties without using their names so instead of 

<p>myGetSomeDataResponse.property1</p>
<p>myGetSomeDataResponse.property2</p>

I would like to do something like 

  <table>
        <tr *ngFor="let AgeBandTableData of myGetSomeDataResponse">
            <td>{{AgeBandTableData.PolicyNumber}}</td>
            <td>{{AgeBandTableData.EffectiveDate}}</td>
            <td>{{AgeBandTableData.PolicyStatus}}</td>
            <td>{{AgeBandTableData.PlanName}}</td>
            <td>{{AgeBandTableData.Group}}</td>
            <td><button type="button">Modify</button></td>
        </tr>
    </table>

but without using these property names (PolicyNumber, EffectiveDate etc)
相关问题