【发布时间】:2014-12-08 00:42:11
【问题描述】:
我有一个这样的控制器:
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 函数中;我想写一些应该从$scope.Profile复制“只有公共属性”的东西,即firstname、middlename和lastname。
我试过 angular.copy 和 angular.extend 但是,
angular.extend合并$scope.BillingDetails和$scope.Profile。 所以我在$scope.BillingDetails中得到email和cellphone属性 好吧——我不想要的。angular.copy覆盖$scope.BillingDetails我输了addressline,city和zipcode来自$scope.BillingDetails——我不想要的。
我想要我的 update 函数做的是它应该使 $scope.BillingDetails 等于下面的对象:
{
firstname : 'Ruchir',
middlename : 'Shakun',
lastname : 'Gupta',
addressline : undefined,
city : undefined,
zipcode : undefined
}
这个场景只是一个例子。为了缩短我的问题的长度,我只提到了 5-6 个属性。事实上,我必须处理 20 多个属性,而且都是动态的。因此,将 firstname、middlename 和 lastname 从 Profile 复制到 BillingDetails 的一对一属性对我不起作用。
我能做什么?
【问题讨论】:
-
$scope.BillingDetails.firstname = $scope.Profile.firstname怎么样? -
您可以创建一个具有所需属性的附加 json 对象并设置值。所以你可以
$scope.BillingDetails扩展这个额外的 json 对象。也许不漂亮,但它可以提供帮助。
标签: javascript angularjs angularjs-directive angularjs-scope angularjs-service