在我的Angular应用中,我使用$ odataresource指令以以下方式使用OData v4 feed:

http://10.0.0.4:8080/InformationProduct?$expand=DataEntities($expand=DataSources)


此查询在Fiddler,Postman和任何现代浏览器中均可正常运行:

{
    @odata.context: "http://10.0.0.4:8080/$metadata#InformationProduct",
    value: [{
        ID: 1,
        Name: "ODM Dashboard",
        Description: "ODM Dashboard",
        Governance_ID: 1,
        PerformanceMetric_ID: 1,
        DataEntities: [{
            ID: 1,
            Name: "Data Entity 1",
            Description: "Data Entity 1",
            InformationProduct_ID: 1,
            BiMeasure_ID: null,
            BiFact_ID: null,
            BiDimension_ID: 1,
            DataSources: [{
                ID: 40,
                Category: "Service Performance",
                SourceSystemName: "Account Improvement Plan",
                SourceSystemOwner: null,
                SourceSystemLocation: null,
                SourceSystemTeam: null,
                SourceSystemNetworkSegment: null,
                SourceSystemOsType: null,
                SourceDatabaseName: null,
                SourceDatabaseType: null,
                SourceDatabaseVersion: null,
                BiFact_ID: null
            }]
        }]
    }]
}


我正在尝试在我的Angular控制器中实现相同的查询,如下所示:

function getData(){
    $odataresource("http://windows-10:8080/InformationProduct")
        .odata()
        .expand('SourceTools')
        .expand('DataEntities','DataSources')
    {}
}


我收到此错误:

GET http://windows-10:8080/InformationProduct?$expand=SourceTools,DataEntities/DataSources 400 (Bad Request)


显然,$ odataresource不会翻译.expand('DataEntities','DataSources')

按预期进入$expand=DataEntities($expand=DataSources)

使$ odataresource指令产生这样的嵌套$ expand的正确方法是什么?

最佳答案

您需要使用isodatav4属性的docs状态让您知道您正在使用v4端点:


  要启用此行为,请在以下情况下将isodatav4属性设置为true
  调用$ odataresource方法:


User = $odataresource('/user', {}, {}, {
    odatakey: 'id',
    isodatav4: true
});


然后使用如下所示的expand方法:

var result = User.odata().expand("roles", "role").query();
//  /user?$expand=roles($expand=role)


对您来说,这意味着您已经拥有正确的扩展调用,只需将isodatav4标志添加到$odataresource("http://windows-10:8080/InformationProduct")即可使$odataresource("http://windows-10:8080/InformationProduct", {}, {}, { isodatav4: true })

10-05 21:46