我们可以向产品阵列添加新项目并使用NGStorage存储这些值,我们可以切换路线,新项目仍然按预期存储但如果我们返回并添加另一个新项目,则会清除之前添加的所有项目。如果有人能看到我们做错了什么,请指出。谢谢。
$scope.Products = [];
$scope.logItem = function($index,brandName,partNumber,productName,amount){
alert("Item Logged");
$scope.newItem = {
Brand: brandName,
Part: partNumber,
Product: productName,
Amount: amount
};
$scope.Products.push($scope.newItem);
$scope.$storage.Products = $scope.Products;
}
答案 0 :(得分:0)
好吧,您将对Products数组的引用传递给存储,然后保存。但是在这里你清除你的数组,从而清除你引用的存储:
$ scope.Products = [];
答案 1 :(得分:0)
当控制器启动时,您需要将$ storage产品重新分配给$ scope.products
//$scope.Products = [];
$scope.Products = $scope.$storage.Products == undefined ? [] : $scope.$storage.Products; // Not exactly: like that when the controller gets initilized, you need to re-assign the $storage products to $scope.products
$scope.logItem = function($index,brandName,partNumber,productName,amount){
alert("Item Logged");
$scope.newItem = {
Brand: brandName,
Part: partNumber,
Product: productName,
Amount: amount
};
$scope.Products.push($scope.newItem);
$scope.$storage.Products = $scope.Products;
}