将另一个(相关的)数组移入.json后,数据不再被推入数组内

时间:2016-03-05 14:21:21

标签: angularjs json

我有this plnkr,使用数组。它有some problems(数据被推入两个数组),但它正在完成它的工作。现在我将该数组移动到.json中,$scope.pizze不再被推入$scope.orderListorderList。我想在这一点上,一个好的解决方案是创建第二个.json并将其用作我的新app.factory('elencoPizze', function($http) { return { getdata: function() { return $http.get('json/pizze.json'); } }; }); app.controller('showcaseCtrl', function($scope, $timeout, elencoPizze) { $scope.pizze = []; elencoPizze.getdata().success(function(data) { $scope.pizze = data; }); return; $scope.orderList = []; $scope.add = function(pizza) { $scope.placeholder = 'Aggiungi altro?'; $scope.empty = false; if ($scope.orderList.indexOf(pizza) === -1) { pizza.n = 1; return $scope.orderList.push(pizza); } else { return pizza.n += 1; } }; return $scope.remove = function(pizza) { var lastRemoving; if (pizza.n <= 1) { pizza.n = 0; lastRemoving = function() { return $timeout((function() { var index; pizza.n = ''; index = $scope.orderList.indexOf(pizza); $scope.orderList.splice(index, 1); if ($scope.orderList.length === 0) { $scope.example(); return $scope.empty = true; } }), 300); }; return lastRemoving(); } else { return pizza.n -= 1; } }; }); // --- // generated by coffee-script 1.9.2 ,或者我错了?顺便问一下,我该怎么做或更好的解决方案?这是更新的代码。

kebab case

1 个答案:

答案 0 :(得分:0)

您尚未向工厂退回承诺。改为:

return {
    getdata: function() {
        return $http.get('json/pizze.json').then(function(data) {
            return data;
        })
    }
};