使用AngularJS,AngularFire和Firebase作为新用户,我遇到了分布式代码库的问题。我试图经常使用Factory()重用代码并按照最佳实践进行测试。但是,我无法将数据添加到我的AngularFire对象和Firebase。以编程方式将字段添加到angularfire并与Firebase同步
鉴于以下工厂:
angular.module('MyApp').factory("ItemData", ["$firebaseObject", "$firebaseArray", "GetFireBaseObject",
function($firebaseObject, $firebaseArray, GetFireBaseObject) {
var ItemsRef = GetFireBaseObject.DataURL('Items/');
return {
AllItems: function() {
return $firebaseArray(ItemsRef);
},
OneItem: function(ItemKey) {
var OneItemRef = ItemsRef.child(ItemKey);
return $firebaseObject(OneItemRef);
}
};
}
]);
我可以让我的项目,我可以用数据进行工作,等等......不过,我似乎无法元素添加到对象,并将其添加到Firebase。当我调用AddQuantity/MinusQuantity时,屏幕将更新数量,但我无法获取该数据与Firebase链接并更新那里的记录。
angular.module('MyApp').controller('InventoryCtrl', ["$scope", "StoreData", "ItemData"
function ($scope, StoreData, ItemData) {
$scope.StoreList = StoresData.AllStores();
$scope.SelectedStore = {}; // Store Information
$scope.ItemList = {}; // Store Item List
$scope.GetItemsForStore = function() {
// StoreDate.OneStoreItems returns list of items in this store
var ItemData = StoreItems.OneStoreItems($scope.SelectedStore.Key); // $firebaseArray() returned
for(var i = 0; i < ItemData.length; ++i)
{
var Item = ItemData[i];
// Item Data is master item data (description, base price, ...)
var OneItem = ItemsData.OneItem(Item.$id); // Get Master by Key
Item.Description = OneItem.Description; // From Master
Item.UnitPrice = OneItem.UnitPrice;
Item.Quantity = 0;
Item.UnitTotal = 0;
}
$scope.ItemList = ItemData;
};
$scope.AddQuantity = function(item) {
if(! item.Quantity) {
item.Quantity = 0;
}
++item.Quantity;
};
$scope.MinusQuantity = function(item) {
if(! item.Quantity) {
item.Quantity = 0;
}
--item.Quantity;
if(item.Quantity <= 0) {
item.Quantity = 0;
}
};
}
]);
从HTML
摘录
<pre>{{ ItemList | json}}</pre>
<div>
<table class="table">
<thead>
<th>Product Code</th>
<th>Description</th>
<th>Qty</th>
<th> </th>
<th> </th>
<th>Extended</th>
</thead>
<tbody>
<tr ng-repeat="(Key, item) in ItemList">
<td>{{item.$id}}</td>
<td>{{item.Description}}</td>
<td>{{item.Quantity}}</td>
<td><button class="btn btn-success" ng-click="AddQuantity(item)">+</button></td>
<td><button class="btn btn-danger" ng-click="MinusQuantity(item)">-</button></td>
<td>{{item.UnitPrice | SLS_Currency}}</td>
<td>{{item.UnitTotal}}</td>
</tr>
</tbody>
</table>
</div>
我可以看到数量字段添加到元素中的ITEMLIST对象,但即使我从纳克重复传递“项目”元素与按钮,这似乎是适当的参考,我没有看到数据同步到Firebase。
将'$ firebaseObject加载到'OneItem'中,然后修改'Item'上的字段。 'var OneItem = ItemData.OneItem(Item。$ id);'vs'Item.Description = OneItem.Description;'。修改'OneItem'上的属性,然后调用'$ save()'。 –
@FrankvanPuffelen我用评论更新了代码,以更好地解释我正在尝试做什么。数据被标准化,并且每个商店可能具有与主项目列表不同的信息子集。 OneItem调用是为Master项目的,所以我可以从中获得描述,价格等等,用于我想要更新的工作列表。我不想更改主项目,但是商店项目。 –
就我所见,问题仍然存在:您需要在'$ firebaseObject'某处调用'$ save'来保存您的更改。如果不是这样,你可以设置一个jsfiddle/jsbin来重现问题吗? –