How can I denote which input fields have changed in AngularJS

前端 未结 7 1134
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 04:14

I\'m working on an edit form (user.html) that PUTs data to an API, but I\'d like to avoid PUTting all the data in the form. I\'d like to PUT just the changed items.

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 04:46

    old thread but to build on TheSharpieOne's answer, you may want to check for equality using angular.equals instead of "===" otherwise this won't work for arrays.

    function findDiff(original, edited){
      var diff = {}
        for(var key in original){
          if(!angular.equals(original[key], edited[key]))
            diff[key] = edited[key];
        }
        return diff;
    }
    

提交回复
热议问题