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

时间:2014-10-10 17:15:35

标签: javascript arrays angularjs object angularjs-scope

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

这是代码:

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

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

2 个答案:

答案 0 :(得分: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;
        });
    }

答案 1 :(得分: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;
    });
}

或使用callBack函数

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