如何使用Restangular从REST服务有效地解析嵌入的_link对象

时间:2015-05-26 10:55:44

标签: angularjs restangular

我有一个REST服务,它返回这样的对象:

{
  "entityVersion" : 0,
  "creationDate" : "2015-05-24T14:32:08.000+0000",
  "modificationDate" : "2015-05-24T14:32:08.000+0000",
  "creator" : "DUMMY",
  "modifier" : "DUMMY",
  "number" : "Z001/01/2014",
  "issueDate" : "2015-05-24T14:32:08.000+0000",
  "expirationDate" : "2016-05-24T14:32:08.000+0000",
  "status" : "ACTIVE",
  "new" : false,
  "id" : 1,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8182/api/domain/permits/1"
    },
    "busLineDefinitions" : {
      "href" : "http://localhost:8182/api/domain/permits/1/busLineDefinitions"
    },
    "carrier" : {
      "href" : "http://localhost:8182/api/domain/permits/1/carrier"
    },
    "schedules" : {
      "href" : "http://localhost:8182/api/domain/permits/1/schedules"
    },
    "permitIssuer" : {
      "href" : "http://localhost:8182/api/domain/permits/1/permitIssuer"
    },
    "vehicles" : {
      "href" : "http://localhost:8182/api/domain/permits/1/vehicles"
    }
  }
}

使用Restangular,如何使用Restangular解析对象'permitIssuer'?这里的关系是一对一的(只有一个许可证颁发者)。

我试过方法:

$scope.getPermitIssuer = function(permitId) {
   var permitIssuerName;
   Restangular.one("permits", permitId).one('permitIssuer').get().then(function(permIssuer){
        permitIssuerName = permIssuer.name;
   });
   return permitIssuerName;
}

但它会返回错误:

错误:[$ rootScope:infdig] http://errors.angularjs.org/1.3.15/ $ rootScope / infdig?p0 = 10& p1 =%5B%5D

任何人都可以告诉我如何最好地使用Restangular来解决这些嵌入式依赖项吗?

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了一种方法:

getPermitIssuer = function(permitId) {
   Restangular.one("permits", permitId).one('permitIssuer').get().then(function(permIssuer){
        $scope.permitIssuerName = permIssuer.name;
   });
}