Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ng repeat child中不起作用的Restangular函数_Javascript_Angularjs_Restangular - Fatal编程技术网

Javascript 在ng repeat child中不起作用的Restangular函数

Javascript 在ng repeat child中不起作用的Restangular函数,javascript,angularjs,restangular,Javascript,Angularjs,Restangular,我在AngularJS应用程序中使用Restangular与后端API通信。在下面的示例中,样式还不是真正一流的,但是因为我的问题与html无关,所以这并不重要。我的问题是change\u status()函数(以及其他函数)只对父对象(todo)有效,而对子对象(child)无效。因此,调用change\u status(todo)可以很好地工作,但是调用change\u status(child)会给我一个todo.put不是一个函数错误。我不明白这一点:传入父对象应该产生与传入子对象完全相

我在AngularJS应用程序中使用Restangular与后端API通信。在下面的示例中,样式还不是真正一流的,但是因为我的问题与html无关,所以这并不重要。我的问题是
change\u status()
函数(以及其他函数)只对父对象(
todo
)有效,而对子对象(
child
)无效。因此,调用
change\u status(todo)
可以很好地工作,但是调用
change\u status(child)
会给我一个
todo.put不是一个函数
错误。我不明白这一点:传入父对象应该产生与传入子对象完全相同的结果,因为两者都是相似的对象(参见示例JSON)

我不能提供一个有效的例子,因为这个错误依赖于与API的通信。但是,我已将所有相关代码包括在下面:

HTML:

示例JSON(在ng repeat中使用):


Children是一个普通数组,未重新设置为Angular,因此您没有重新设置为Angular的方法,您必须重新设置为Angular(检查restangularizeCollection方法),才能将其用于重新设置为Angular的方法(put)。
由于父数组是通过restangular调用检索的,因此它已经被重新格式化。

我使用了customPUT方法,它不需要我重新格式化所有对象(这将非常困难):


我如何做到这一点而不必手动重新对每个子项进行语法化?您是否可以将子项作为第二个重新语法化调用(类似于父项todo的嵌套rest调用)检索?这将变得非常混乱,因为我有一些深度嵌套的子项(我的JSON示例已简化)。如果我必须手动调用所有子项,我的代码将变得非常混乱且无法维护。它不应该真的,现在你在todo.children中有children,好的,所以不要查看todo.children,您应该使用todo.getChildren或类似的函数,该函数将返回一个restangular调用来检索todo的子对象(类似于todo.getList(children))。另外,这将只加载您需要按需加载的内容,而不是一次加载所有信息。无论如何,我可能在这里过于复杂了,您只有一个资源,对吗?这将带来TODO的整个层次结构,因此更简单,只需始终在顶层执行put方法,这也会更新子级。
...
<tbody ng-repeat="todo in todos | filter:search" ng-hide="todo.hidden">
    <tr>
        <td style="width:100px" ng-click="change_status(todo)">
            <span ng-if="todo.done" class="label label-success">Done</span>
            <span ng-if="!todo.done" class="label label-warning">In Progress</span>
        </td>
        ...
    </tr>
    <tr ng-repeat="child in todo.children | filter:search" ng-hide="child.hidden">
        <td style="width:100px" ng-click="change_status(child)">
            <span ng-if="child.done" class="label label-success">Done</span>
            <span ng-if="!child.done" class="label label-warning">In Progress</span>
        </td>
        ...
    </tr>
</tbody>
...
...
$scope.change_status = function(todo) {
        todo.done = !todo.done;
        todo.put();
        $rootScope.todos = api.all('todo').getList().$object;
    };
...
[
  {
    "description": null,
    "title": "Learn to make a great todo-app using AngularJS",
    "children": [
      {
        "description": null,
        "title": "Learn AngularJS",
        "children" : [],
        "done": false,
        "user": 1,
        "hidden": false,
        "id": 2
      }
    ],
    "done": false,
    "user": 1,
    "hidden": false,
    "id": 1
  }
]
Restangular.allUrl('edittodo/' + todo.id).customPUT(todo)