AngularFire:测试 $firebaseObject 是否没有键的最佳实践是什么?
AngularFire: What is the best practice to test if $firebaseObject has no keys?
简单 JavaScript object 当 angular.equals({}, myObj) === true
时为空(= 没有键)。
是否有一些实用函数可以检测$firebaseObject
是否没有钥匙?它永远不会等于 {}
因为它看起来像这样:
{
$$conf: Object,
$id: "objectId",
$priority: null,
$value: null
}
编辑:我试图在 $firebaseObject
没有数据时隐藏 <div>
。这似乎可以解决问题,但我不知道这是否是正确的解决方案:
<div ng-hide="myObj.$value === null">
[...]
</div>
来自 the doc:
If the value in the database is a primitive (boolean, string, or
number) then the value will be stored under this $value
key. Modifying
this value and then calling $save()
will also update the server’s
value.
Note that any time other keys exist, this one will be ignored. To
change an object to a primitive value, delete the other keys and add
this key to the object.
[...]
我的 $firebaseObject
有 children 这样的:
key: {
value1: 'something',
value2: 'something'
}
当我玩 $value
时,我发现了这个:
- 当
$firebaseObject
没有child时,$value
就是null
- 当
$firebaseObject
有一个child时,$value
是它的键
- 当
$firebaseObject
多于一个child时,$value
包含所有children objects
问题是,我可以使用 $value
这种方式来解决我的问题,还是不可能因为...?
因为这是在控制器中绑定数据的方式:
function MainController($firebaseObject) {
var ref = new Firebase('https://yours.firebaseio.com');
vm.users = $firebaseObject(ref.child('users'));
这适用于 show/hide:
<div class="small-12 column" ng-show="vm.users">
<h1>No user</h1>
</div>
<div class="small-12 column" ng-show="!vm.users">
<h1>Users</h1>
<ul>
<li ng-repeat="user in vm.users">{{user}}</li>
</ul>
</div>
顺便说一句:由于您在用户上使用 ng-repeat
,因此您需要将其设为 $firebaseArray()
。
简单 JavaScript object 当 angular.equals({}, myObj) === true
时为空(= 没有键)。
是否有一些实用函数可以检测$firebaseObject
是否没有钥匙?它永远不会等于 {}
因为它看起来像这样:
{
$$conf: Object,
$id: "objectId",
$priority: null,
$value: null
}
编辑:我试图在 $firebaseObject
没有数据时隐藏 <div>
。这似乎可以解决问题,但我不知道这是否是正确的解决方案:
<div ng-hide="myObj.$value === null">
[...]
</div>
来自 the doc:
If the value in the database is a primitive (boolean, string, or number) then the value will be stored under this
$value
key. Modifying this value and then calling$save()
will also update the server’s value.Note that any time other keys exist, this one will be ignored. To change an object to a primitive value, delete the other keys and add this key to the object.
[...]
我的 $firebaseObject
有 children 这样的:
key: {
value1: 'something',
value2: 'something'
}
当我玩 $value
时,我发现了这个:
- 当
$firebaseObject
没有child时,$value
就是null
- 当
$firebaseObject
有一个child时,$value
是它的键 - 当
$firebaseObject
多于一个child时,$value
包含所有children objects
问题是,我可以使用 $value
这种方式来解决我的问题,还是不可能因为...?
因为这是在控制器中绑定数据的方式:
function MainController($firebaseObject) {
var ref = new Firebase('https://yours.firebaseio.com');
vm.users = $firebaseObject(ref.child('users'));
这适用于 show/hide:
<div class="small-12 column" ng-show="vm.users">
<h1>No user</h1>
</div>
<div class="small-12 column" ng-show="!vm.users">
<h1>Users</h1>
<ul>
<li ng-repeat="user in vm.users">{{user}}</li>
</ul>
</div>
顺便说一句:由于您在用户上使用 ng-repeat
,因此您需要将其设为 $firebaseArray()
。