2014-10-10 107 views
1

如何使对象继承其他对象的所有属性。角度对象如何获取另一个对象的值

这是代码:

this.makeReady = function(order) { 
    var tempOrder = angular.copy(order); 
    tempOrder.status = 1; 
    angular.forEach(tempOrder.items, function(item){ 
     item.status = 1; 
    }) 
    return $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){ 
     order = tempOrder; // this doesn't work 
    }); 
} 

在成功的情况下:改变该对象的值。

+0

你传递给makeReady函数的order参数是什么?如果它是$ scope.order,那么只需在你的成功函数中使用'$ scope.order = tempOrder;'。 – adam0101 2014-10-10 18:05:01

+0

'$ scope.allOrders'包含所有订单,所以当我更改'订单'时,它将影响所有订单.. – 2014-10-10 21:03:33

回答

1

尝试在你的$scope.allOrders直接编辑的顺序,看看是否能得到您的行为你正在寻找。

this.makeReady = function (order) { 
     var tempOrder = angular.copy(order); 
     var orderIndex = $scope.allOrders.indexOf(order); 
     tempOrder.status = 1; 

     angular.forEach(tempOrder.items, function(item) { 
      item.status = 1; 
     }); 

     return $http.put('/rest/change/invoice/' + order.id + '/', tempOrder).success(function() { 
      $scope.allOrders[orderIndex] = tempOrder; 
     }); 
    } 
+0

它的工作原理是这样的,但我认为它可以完成没有indexOf ..谢谢.. – 2014-10-10 21:25:39

0

使用本

this.makeReady = function(order) { 
    var tempOrder = angular.copy(order); 
    tempOrder.status = 1; 
    angular.forEach(tempOrder.items, function(item){ 
     item.status = 1; 
    }) 
    $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){ 
     order = tempOrder; // this doesn't work 
     return order; 
    }); 
} 

或使用回叫功能

this.makeReady = function(order, callback) { 
     var tempOrder = angular.copy(order); 
     tempOrder.status = 1; 
     angular.forEach(tempOrder.items, function(item){ 
      item.status = 1; 
     }) 
     $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){ 
      order = tempOrder; // this doesn't work 
      callback(order) 
     }); 
    }; 

通话功能

this.makeReady({status:1, data:2, items:{status:1}}, function(data){ 
// this data your order variable in service 
}) 
+0

我不确定您的第一个示例是否按预期工作。 $ http.put不同步。回调示例应该可以工作。 – adam0101 2014-10-10 18:03:29

+0

试着说我好吗? – 2014-10-10 18:08:56

+0

第一个例子不起作用..第二个例子不适合我的逻辑,因为当我更新这个订单时,它会在allOrders变量上更新..第一个例子会很好,如果工作.. – 2014-10-10 21:02:49

相关问题