【问题标题】:AngularJS NgResource API callAngularJS NgResource API 调用
【发布时间】:2016-03-18 00:36:16
【问题描述】:

所以我目前正在使用 $ngResouce。我的 api: api/Content/resource/user.post.failed 返回以下内容:

{
  "user.post.failed": "Thing.."
}

app.factory('testResource', function ($resource) {
return $resource(apiurl + '/Content/resource/:resourcename', {}, {
    show: { method: 'GET', isArray: false, params: {resourcename: '@resourcename'} },
    update: { method: 'PUT', params: {id: '@id'} },
    delete: { method: 'DELETE', params: {id: '@id'} }
})

});

这就是我在控制器中调用的内容

$scope.test = testResource.get({resourcename: 'test'});

这个问题其实很简单;我怎样才能得到'内容部分'所以测试。我现在要取回整个 JSON 部分。

所以现在的范围测试是:{"user.post.failed":"Thing.."} 我希望范围只是 Thing。

可能真的很简单,但我找不到答案。

【问题讨论】:

    标签: angularjs json api rest get


    【解决方案1】:

    使用对象的$promise 属性查看错误:

    $scope.test = testResource.get({resourcename: 'test'});
    
    $scope.test.$promise.catch(function onReject(response) {
        console.log('ERROR: ',response.status);
        console.log(response);
    });
    

    所以现在的范围测试是:{"user.post.failed":"Thing.."} 我希望范围只是 Thing。

     console.log($scope.test["use.post.failed"]);
    

    使用属性访问器语法。

    更多信息见MDN JavaScript Reference -- Property Accessors

    HTML

    {{ test["use.post.failed"] }}
    

    【讨论】:

    • 将此标记为答案。由于此答案提供了更多信息;)感谢你们俩的回答。
    【解决方案2】:
    testResource.get({resourcename: 'test'}, function(data) {
        $scope.test = data['user.post.failed'];
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2014-05-03
      • 2023-03-22
      相关资源
      最近更新 更多