angularjs - 将一个对象的公共属性复制到另一个对象

时间:2014-10-13 12:36:55

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-service

我有一个像这样的控制器:

CheckoutController = function() {
    $scope.Profile = {
        firstname : 'Ruchir',
        middlename : 'Shakun',
        lastname : 'Gupta',
        email : 'ruchir@example.com',
        cellphone : '9876543210'
    }

    $scope.BillingDetails = {
        firstname : undefined,
        middlename : undefined,
        lastname : undefined,
        addressline : undefined,
        city : undefined,
        zipcode : undefined
    }

    $scope.update = function() {
        // I want to write some awesome code here as explained below
    }
}

现在,在$scope.update函数中;我想写一些应该复制' 只有常见属性'即从firstnamemiddlename的{​​{1}},lastname$scope.Profile

我尝试了$scope.BillingDetailsangular.copy但是,

  • angular.extend合并angular.extend$scope.BillingDetails。 我在$scope.Profile中获得了emailcellphone个属性 好吧 - 我不想要的。

  • $scope.BillingDetails覆盖 angular.copy我遗失了$scope.BillingDetailsaddressline和 来自city的{​​{1}} - 我不想要的。

我希望zipcode函数执行的操作是$scope.BillingDetails等于下面的对象:

update

这个场景只是一个例子。为了缩短我的问题的长度,我只提到了5-6个属性。事实上,我必须处理超过20个属性,而且都是动态的。因此,通过将$scope.BillingDetails{ firstname : 'Ruchir', middlename : 'Shakun', lastname : 'Gupta', addressline : undefined, city : undefined, zipcode : undefined } firstnamemiddlename复制到lastname,我无法为我工作。 我该怎么办?

3 个答案:

答案 0 :(得分:13)

你可能会有这样的运气:

$scope.update = function() {
  _update($scope.Profile, $scope.BillingDetails);
}

function _update(srcObj, destObj) {
  for (var key in destObj) {
    if(destObj.hasOwnProperty(key) && srcObj.hasOwnProperty(key)) {
      destObj[key] = srcObj[key];
    }
  }
}

plunker

答案 1 :(得分:4)

简单。只需像这样分配它们:

$scope.update = function() {
    $scope.BillingDetails.firstname = $scope.Profile.firstname;
    $scope.BillingDetails.middlename = $scope.Profile.middlename;
    $scope.BillingDetails.lastname = $scope.Profile.lastname;
}

我真的想不出将一个属性从一个对象复制到另一个对象的更直接的方法。

由于您需要复制3个以上的属性,您可以尝试这样做:

$scope.update = function() {
    // Add the properties you want to copy to this array.
    var properties = ['firstname', 'middlename', 'lastname'];
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

或者,将数组作为参数传递:

$scope.update = function(properties) {
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

$scope.update(['firstname', 'middlename', 'lastname']);

答案 2 :(得分:3)

事实上,您尝试使用BillingDetails的值更新Profile,因为它们都具有相同的属性吗?

如果您可以使用null而不是undefined更改BillingDetails的默认值,则可以尝试以下代码:

$scope.BillingDetails = {
    firstname : null,
    middlename : null,
    lastname : null,
    addressline : null,
    city : null,
    zipcode : null
}

$scope.update = function() {
    for(var key in $scope.Profile) {
        if(typeof $scope.BillingDetails[key] !== 'undefined') {
            $scope.BillingDetails[key] = $scope.Profile[key];
        }
    }
}
相关问题