使用Restangular将嵌套数组中的项添加到Angular.js中的对象中

时间:2014-03-09 18:41:08

标签: javascript angularjs restangular angularjs-controller

我有一个模特。该模型有很多潜在的嵌套数组。该项目是一项任务,我可以为其添加一个类别。

当我正在做一个新的.POST时如何将嵌套数组中的项目放入模型中?我查看了Restangular文档,其中说明了处理嵌套模型很容易(这就是我选择Restangular的原因),但我没有看到一个简单的方法。我知道我只是没有看到我面前可能正确的东西,所以我在这里问,所以有人可以告诉我“正确”的方式。

这是我一直在研究的控制器。我无法弄清楚如何将类别放入数组中以执行任务。我是否需要ng-click或将资源URL传递到控制器中带有.push()的数组?

TaskCtrls.controller('TaskAddCtrl', ['$scope', 'Restangular', '$state', 'growl',
    function($scope, Restangular, $state, growl) {

        // Adding in the need meta objects
        Restangular.all('category').getList().then(function(categories){
            $scope.categories = categories;
        }, function(response) {
            growl.addErrorMessage("There was an error with status code: ", response.status);
        });

        $scope.task = {} // do I add the category here? And how do I do that?

        $scope.addNote = function() {
            Restangular.all('task/').post($scope.task).then(function(task){
                console.log(task);
                growl.addSuccessMessage("Your task has been added!");
                $state.go('tasks.list');
            }, function(response) {
                console.log(response.data);
                growl.addErrorMessage("There was an error with status code: ", response.status);
            });
        }
    }
]);

0 个答案:

没有答案