javascript - "Object doesn' t 支持属性或方法 'push' "

标签 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 中,添加 status 属性后看起来像这样:

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

最佳答案

.push()是JS数组的一个方法。

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

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

关于javascript - "Object doesn' t 支持属性或方法 'push' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262699/

相关文章:

javascript - Jsfiddle 的新 UI 打破了我的 Angular 和 javascript fiddle

javascript - Controller 的 Angular stateParams,$urlRouterProvider

javascript - 网格系统不显示文档中的颜色

javascript - AngularJS 在 ng-change 调用的函数中将属性值作为参数传递

javascript - 我应该如何在不使用范围的情况下在我的 Controller 功能中引用服务?

javascript - 如何在两个独立的后端之间同步数据?

javascript - 如何在 Angular js 中的 .run 方法内调用读取 json 文件的工厂?是否可能?

javascript - 当asp.net页面有特定查询字符串时调用JQuery函数

javascript - Mocha 抛出 "Resolution method is overspecified"

javascript - Angular - 无法观察工厂变量