jth*_*ley 1 firebase angularfire
对不起这个长度,但它让我有点疯狂:
假设我想在加载时获得项目的"标题"和".priority"键值.
首先让我们看看每件事的布局:
$scope.items = $firebase(new Firebase("https://****.firebaseio.com"));
$scope.items.$on('loaded', function() {
console.log($scope.items);
});
Run Code Online (Sandbox Code Playgroud)
有了这个,我们得到:
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> this is my item: Object
$$hashKey: "012"
$id: "this is my item"
$priority: 2884707
title: "this is the title of my item"
__proto__: Object
> __proto__: Object
Run Code Online (Sandbox Code Playgroud)
看起来不错,让我们尝试抓住优先级:
console.log($scope.items.$child('$priority'));
Run Code Online (Sandbox Code Playgroud)
哎呀:
Uncaught Error: Firebase.child failed: First argument was an invalid path: "$priority". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
Run Code Online (Sandbox Code Playgroud)
好的,我们暂时跳过这个,只是尝试标题:
console.log($scope.items.$child('title'));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
> __proto__: Object
Run Code Online (Sandbox Code Playgroud)
无题.
好的,让我们以不同的方式尝试:
var firstItem = $scope.items.$getIndex()[0]; //returns $ids in order of priority
console.log($scope.items.$child(firstItem));
> Object {$bind: function, $add: function, $save: function, $set: function, $remove: function…}
> $add: function (item, cb) {
> $bind: function (scope, name) {
> $child: function (key) {
> $getIndex: function () {
> $on: function (type, callback) {
> $remove: function (key) {
> $save: function (key) {
> $set: function (newValue) {
title: "this is the title of my item"
> __proto__: Object
Run Code Online (Sandbox Code Playgroud)
但现在没有优先权!
厨房水槽在计算机时间发誓:
console.log($scope.items.title);
> undefined
console.log($scope.items[0].title);
> Uncaught TypeError: Cannot read property 'title' of undefined
console.log(Object.keys($scope.items));
> ["$bind", "$add", "$save", "$set", "$remove", "$child", "$on", "$getIndex"]
Run Code Online (Sandbox Code Playgroud)
我错过了什么?我可以通过做几次掉头来获得'头衔',(弄清楚该项目的$ id是使用'$ getIndex()',抓住第n个项目,然后使用'$ child()'再次打电话),但即便如此也不适合优先考虑.是否有更简单的方法从AngularFire v2中获取关键值?或者以任何方式获得$优先权?
我相信我遇到了同样的问题.希望这与您的经历有关.即:
$scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
$scope.items.$on('loaded', function() {
console.log($scope.items['some key']); // this writes undefined to console
});
Run Code Online (Sandbox Code Playgroud)
我注意到的是,如果我不依赖这个事件,这确实有效loaded.例如在HTML中:
<a href="#" data-ng-click="logItem()">click me</a>
Run Code Online (Sandbox Code Playgroud)
在JS中:
$scope.items = $firebase(new Firebase("https://xxxxxx.firebaseio.com"));
$scope.logItem = function() {
console.log($scope.items['some key']); // this works
}
Run Code Online (Sandbox Code Playgroud)
单击"单击我"链接会按预期将我的项目记录到控制台.
感觉像loaded时间的集合没有完全填充firebase的基础项目.
| 归档时间: |
|
| 查看次数: |
10749 次 |
| 最近记录: |