如何保存火力地堡对象$ asArray()与angularFire,当对象包含NG-repetate [英] How to save Firebase objects $asArray() with angularFire, when the objects contain ng-repetate
问题描述
我最近从angularfire 0.6切换到到0.8.0。我有问题,保存清单的项目,包含数组本身。
I recently switched from angularfire 0.6 to to 0.8.0. I am having problems to save an item of a list, that contains an array itself.
我的对象的帐户是这样的:
{
"-JQruasomekeys0nrXxH" : {
"created" : "2014-03-23T22:00:10.176Z",
"durations" : [ {
"$$hashKey": "00L", // this is generated by ng-repeat on arrays
"end" : "2014-07-15T22:00:00.000Z",
"start" : "2014-07-09T22:00:00.000Z"
} ],
"email" : "some@mail.net",
}
}
的持续时间是与开始和结束的时间段,这是由在HTML中两个输入场的纳克重复类似的阵列
The durations are an array of time periods with start and end, which are resembled by a ng-repeat of two input fields in HTML.
<tr ng-repeat="duration in account.durations">
<td>
<input ng-model="duration.start" datepicker>
</td>
<td>
<input ng-model="duration.end" datepicker>
</td>
</tr>
帐户保存到火力点之前,我做angular.copy($ scope.account)的控制器,摆脱角度$$散列值。这个曾在angularfire 0.6.0。
在angularfire 0.8.0,我仍然得到错误:
In angularfire 0.8.0, I am still getting the error:
错误:Firebase.set失败:第一个参数包含一个无效的键
在财产durations.0'($$ hashKey)。钥匙必须是非空字符串
并且不能包含。,#,$,/,[,或]
Error: Firebase.set failed: First argument contains an invalid key ($$hashKey) in property 'durations.0'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]
这是怎么认为由angularfire解决,使用对象的时候,有自己的内部数组?持续时间将不会是唯一的数组我将有帐户中。那么,有没有一个更专业的解决方案,或者我一定要angular.copy每一个数组对象通过angularfire保存到火力过吗?
How is this meant to be solved by angularfire, when working with objects, that have arrays within themselves? Durations won't be the only array I will have within the account. So is there a more professional solution, or do i have to angular.copy every single array object before saving to firebase via angularfire?
感谢您事先的任何暗示。
Thanks in advance for any hint.
更新(11月14)
做了一些更多的研究后,问题不在于使用angular.copy()不工作了。实际上它的作用。但它是非常不方便更新/修改与angularfire方法$保存(现有数据集)(的https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-save)因为我似乎无法找到$一个适当的方式保存()一个项目做angular.copy()之后。
Update (11. Aug 14) After doing a bit more research, the Problem is not that using angular.copy() does not work any more. actually it does. But it is very unhandy to update/modify an existing dataset with the angularfire method $save() (https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-save) Because I can't seem to find a proper way to $save() an item after doing angular.copy().
更新2(14年8月12日)
我发现的时刻要解决此问题,但实际上一个解决方法是什么,我想避免在首位:
Update 2 (12 Aug 14) I found a workaround for the moment, but actually a workaround is what I wanted to avoid in the first place:
// loop for every duration, that should clean the object from "$$hashKey" attributes
angular.forEach($scope.account.durations, function (value, index) {
var cleanValue = {};
// checks, if the date is a JSON string already
if (typeof value.start === 'string') {
cleanValue.start = value.start;
} else {
cleanValue.start = value.start.toJSON();
}
// checks, if the date is a JSON string already
if (typeof value.end === 'string') {
cleanValue.end = value.end;
} else {
cleanValue.end = value.end.toJSON();
}
// overwrite the array object at index to get loose of $$hashKey
$scope.account.durations[index] = cleanValue;
});
我认为,这一解决办法需要的优势,在关闭的火力地堡基于文件的原则,因为我已经通过AngularFire存放前,要知道一个对象的准确属性。
I think that this workaround takes the advantages off the document based principle in Firebase, as I have to know the exact attributes of an object before storing it via AngularFire.
更新3(13月14)
我加了的jsfiddle显示问题: jsfiddle.net/1fem6byt
如果您添加行到数组,然后尝试和对象保存到火力点,在$$ hashKey错误(上述)出现在控制台中。有办法解决这个问题,但是,我期待与angularfire更容易或更清洁的解决方案,如果可能的话。我可能不会做加线的正确 - ?还是我错过的东西。
If you add lines to the array, and then try and save the object to the firebase, the $$hashKey error (given above) appears in the console. There are ways to workaround this problem, but I am looking for an easier, or cleaner solution with angularfire, if possible. I might not do adding of lines correctly – or do I miss something?
推荐答案
当然解决它像 @Kato火力的最佳方式提及,并有实时从数组绑定到数据眼帘,你应该使用通过$ asArray实例同步对象一个额外的火力点。
Of course the best way to solve it in firebase like @Kato mentioned and have the real time binding to data from array into view you should use an extra firebase synchronized object through the $asArray instance.
Like in this solution: http://jsfiddle.net/RaVbaker/sb00kor8/1/ But you will loose then possibility to save data "on demand" with button like Save to Firebase
. They will be saved instantly after change in firebase array object occurs. But it will also give you flexibility and synchronization of data across pages.
但是其他的,更容易在你的情况的方式来改变角度的轨道唯一对象中的 NG-重复指令。它可以通过 NG-重复
语句结尾由$指数设置的轨道来完成。
But other and much easier in your case is to change the way angular tracks uniqueness of objects in ng-repeat directive. It can be done by setting track by $index
in the end of ng-repeat
statement.
修改 NG-重复=持续时间account.durations
这个:
ng-repeat="duration in account.durations track by $index"
您的jsfiddle这个解决方案更新时间: http://jsfiddle.net/RaVbaker/sb00kor8/2/
Your jsfiddle updated with this solution: http://jsfiddle.net/RaVbaker/sb00kor8/2/
关于按
轨道更多信息的ngRepeat你可以明显发现的官方文档。
More info about track by
for ngRepeat you can find obviously in official documentation.
希望我帮助。
这篇关于如何保存火力地堡对象$ asArray()与angularFire,当对象包含NG-repetate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!