$scope数据在angular的数组格式

$scope data in array format in angular

本文关键字:数组 格式 angular scope 数据      更新时间:2023-09-26

在angular上有一个测试应用。我尝试在我的模型中从php页面接收数据。响应来自php文件中的命令echo json_encode($arr);,格式为[{"id":"1","name":"first","text":"description"}]。要在我的模型中接收此数据,只需要使用查询cause .get给出错误。我在控制器的查询:$scope.item = Items.query({id:$routeParams.id});现在,如果我想在我的模型中使用这些数据,我需要指定我的数组项[0].name这不是问题,但当我试图保存编辑过的数据

 $scope.item[0].$save({id:$scope.item[0].id});

我有一个错误TypeError:对象#没有方法'push'

我做错了什么?

如果您正确设置了资源,您应该能够通过简单地调用

来保存:
$scope.item[0].$save();

然而,看起来你并没有那样做。当您使用query时,它通常返回一个数组。当您查询单个ID时,您应该真正使用get方法:

$scope.item = Items.get({id:$routeParams.id});
...
// Modify $scope.item as needed
...
$scope.item.$save();
定义资源最简单的方法是使用Angular提供的默认动作:
.factory('Items', function($resource) {
    return $resource('/angular/angular-seed/app/questions/php/maintext.php');
});

根据Angular文档:

The default set contains these actions:
{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };