Why does angular $resource add extra objects ($promise, $resolve…) to my data response?

前端 未结 5 876
感情败类
感情败类 2021-01-17 08:38

I return a resource with a URL

    $resource(\"http://foo.com/bar.json\").get().
         $promise.then(function(data){ $scope.result = data}, 
                     


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 09:08

    $resource returns an object or array that will have your data when the call completes. All those functions are there to help you out and $resource is mainly intended for CRUD operations. If you want the data, you have to wait for it to get returned so you might as well use the promise. If you want to strip all of those properties you can use angular.toJson to convert it to json, but angular does that for you when posting it back to a resource or $http call so you shouldn't have to.

    $scope.data = $resource("http://foo.com/bar.json").get();
    // $scope.data does not have your data yet, it will be
    // populated with your data when the AJAX call completes
    ...
    // later in a call from a save button maybe you can just do
    // this to post your changes back:
    $scope.data.$save();
    

提交回复
热议问题