2014-10-10 129 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

相關問題