programing

angularjs - 확장 재귀적

lovejava 2023. 10. 26. 20:26

angularjs - 확장 재귀적

속성을 재귀적(일명 딥 카피)으로 확장하고 싶습니다.jQuery가 하는 것처럼.저는 단지 한가지의 b/c만 jquery를 포함하지 않습니다.

jQuery.extend( true, target, object1 )

간단한 자바스크립트나 angularjs로 할 수 있는 우아한 방법이 있습니까?

업데이트를 보시고 동일한 결과를 얻기 위해 노력해주세요 http://plnkr.co/edit/GHabYbyhsqtfBPtplksO?p=preview .

.copy ()를 조사했지만 "(객체의) 속성이 삭제되었습니다."

여기 각도를 기반으로 한 extend Deep 함수가 있습니다.기능을 확장시키다이 내용을 $scope에 추가하면 전화를 걸 수 있습니다.

$scope.meta = $scope.extendDeep(ajaxResponse1.myMeta, ajaxResponse2.defaultMeta);

원하는 답을 얻을 수 있습니다.

$scope.extendDeep = function extendDeep(dst) {
  angular.forEach(arguments, function(obj) {
    if (obj !== dst) {
      angular.forEach(obj, function(value, key) {
        if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
          extendDeep(dst[key], value);
        } else {
          dst[key] = value;
        }     
      });   
    }
  });
  return dst;
};

참고: 이 함수는 나중에 인수의 값을 이전 인수로 복사하는 부작용이 있습니다.이 부작용에 대한 간단한 해결을 위해 변경할 수 있습니다.dst[key] = value로.dst[key] = angular.copy(value).

여기 있는 모든 답변은 1.4 이전 버전의 Angular에 대해 유효합니다.

Angular 1.4 기준으로 사용 가능합니다.angular.merge바로 그 일을 하기 위해서는:

extend()와 달리 merge()는 소스 개체의 개체 속성으로 재귀적으로 하강하여 심층 복사를 수행합니다.

https://docs.angularjs.org/api/ng/function/angular.merge

function deepExtend(destination, source) {
  for (var property in source) {
    if (source[property] && source[property].constructor &&
     source[property].constructor === Object) {
      destination[property] = destination[property] || {};
      arguments.callee(destination[property], source[property]);
    } else {
      destination[property] = source[property];
    }
  }
  return destination;
}

플렁커

Src: https://gist.github.com/gregdangelo/2343158

라이언의 코드를 기반으로 객체 검사를 단축할 수 있으며 객체 포인터를 오버라이드하지 않도록 기능을 확장해서는 안 됩니다.

var extendDeep = function extendDeep(dst) {
    angular.forEach(arguments, function(obj) {
        if (obj !== dst) {
            angular.forEach(obj, function(value, key) {
                if (dst[key] && angular.isObject(dst[key])) {
                    extendDeep(dst[key], value);
                } else if(!angular.isFunction(dst[key])) {
                    dst[key] = value;
                }
            });
        }
    });
    return dst;
};

Ryan과 동일한 솔루션이지만 어레이 병합을 지원합니다.

function extendDeep(dst) {
      angular.forEach(arguments, function (obj) {
          if (obj !== dst) {
            angular.forEach(obj, function (value, key) {
                if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
                  extendDeep(dst[key], value);
                } else if (dst[key] && dst[key].constructor && dst[key].constructor === Array) {
                  dst[key].concat(value);
                } else if(!angular.isFunction(dst[key])) {
                  dst[key] = value;
                }
              }
            );
          }
        }
      );
      return dst;
    }

Angular에는 복사 방법이 있습니다.

각진.복사의

언급URL : https://stackoverflow.com/questions/15310935/angularjs-extend-recursive