문제

ngResource에서 $save를 호출할 때 POST가 가능합니까? 오직 매번 전체 모델을 게시하는 대신 편집된 필드를 삭제하시겠습니까?

var User = $resource('http://example.com/user/123/');

User.get(function(user) {
  user.name="John Smith";
  user.$save();
  // What I *want* -> POST: /user/123/ {name:'John Smith'}
  // What currently happens -> POST: /user/123/ {name:'John Smith', age: 72, location: 'New York', noOfChildren: 5}
});
도움이 되었습니까?

해결책

아니요, 적어도 인스턴스에서는 불가능합니다. http://docs.angularjs.org/api/ngResource.$resource

...] 클래스 객체 또는 인스턴스 객체의 동작 메소드는 다음 매개 변수로 호출 할 수 있습니다.

  • HTTP GET "클래스" 작업: Resource.action([parameters], [success], [error])
  • GET이 아닌 "클래스" 작업: Resource.action([parameters], postData, [success], [error])
  • GET이 아닌 인스턴스 작업: instance.$action([parameters], [success], [error])

따라서 저장할 데이터를 "정적" 저장 방법으로 전달해야만 가능합니다. 즉, User.save.이 같은:

User.get(function(user)
{
    user.name = 'John Smith';
    User.save({name: user.name});
});

이것이 당신에게 효과가 있는지 여부는 아마도 당신이 무엇을 할 것인지에 달려 있을 것입니다. user 사례.

다른 팁

하나의 필드 만 저장하려는 경우, 정적 .save() 메서드를 사용하여 응답을 수행하고 성공시 로컬 객체를 업데이트합니다.

$scope.saveOneField = function(modelInstance) {
  ModelName.save({
    id: modelInstance.id,
    theField: <some value>
  }, function(response) {
    // If you want to update *all* the latest fields:
    angular.copy(response, modelInstance.data);
    // If you want to update just the one:
    modelInstance.theField = response.data.theField;
  });
};
.

POST 요청이 리소스 (즉, /modelnames/:id)로 전송 될 때 서버가 최신 업데이트 된 버전의 ModelInstace로 응답한다고 가정합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top