"对象不支持属性或方法' push'"

时间:2014-10-08 17:18:19

标签: javascript angularjs angularjs-scope

我正在尝试将新值添加到AngularJS中的现有JSON对象中,但我收到此错误消息:

"Object doesn't support property or method 'push'"

这是我的代码:

$scope.addStatus = function (text) {          
          $scope.application.push({ 'status': text }); //I have tried 'put' but getting the error
      };

 $scope.create = function( application ){
        $scope.addStatus('Under review');
 }

这是我的应用程序json看起来像:

{"type":"Not completed","source":"mail","number":"123-23-4231","amount":"234.44","name":"John ","id":"123","by_phone":true}

我想在上面的json中添加/添加状态,并在添加状态属性后显示:

{"type":"Not completed","source":"mail","number":"123-23-4231","amount":"234.44","name":"John ","id":"123","by_phone":true, "status": "under review"}

2 个答案:

答案 0 :(得分:3)

.push()是JS Array的一种方法。

但是应用程序只是一个对象(不是数组)。在这里我们可以使用:

// instead of this
// $scope.application.push({ 'status': text });
// use this
$scope.application.status = text;

答案 1 :(得分:1)

Array.prototype.push仅适用于数组。以下列方式之一添加属性:

$scope.application['status'] = text;

或者:

$scope.application.status = text;