一个有角度的对象如何获得另一个对象的值

How an angular object gets the value of another object

本文关键字:对象 何获得 一个对象 一个      更新时间:2023-09-26

如何使对象继承其他对象的所有属性。

这是代码:

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
    });
}

如果成功:更改该对象的值。

试着直接编辑$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;
        });
    }

使用此

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
})