我正在尝试将新值添加到 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/