Javascript 如何在不从firebase重新加载页面的情况下进行数据更新
我正在尝试实现一个简单的收藏夹系统。在页面上,加载帖子列在主页上,任何以前喜欢的帖子(称为NUB)都会在下面显示FAVED标签Javascript 如何在不从firebase重新加载页面的情况下进行数据更新,javascript,html,angularjs,firebase,angularfire,Javascript,Html,Angularjs,Firebase,Angularfire,我正在尝试实现一个简单的收藏夹系统。在页面上,加载帖子列在主页上,任何以前喜欢的帖子(称为NUB)都会在下面显示FAVED标签 <div class="list-group" ng-repeat="nub in nubs"> <a href="#" class="list-group-item active"> <h4 class="list-group-item-heading">{{nub.title}}</h4> <
<div class="list-group" ng-repeat="nub in nubs">
<a href="#" class="list-group-item active">
<h4 class="list-group-item-heading">{{nub.title}}</h4>
<p class="list-group-item-text">{{nub.description}}</p>
<p class="list-group-item-text">{{nub.synopsis}}</p>
<li ng-repeat="url in nub.attachmentsUrls">
<p class="list-group-item-image">
<img ng-src={{url}} />
</p>
</li>
</a>
<button ng-click="toggleFav(nub)">favorite</button>
<p ng-show="getFaved(nub.$id)">FAVED</p>
</div>
本质上,它是通过页面上显示的NUB或POST循环,并根据用户喜欢的NUB检查它们,以显示FAVED标记并切换favorite按钮的功能。如果用户不喜欢nub,按钮会将nub添加到他们的收藏夹列表中,并将其添加到喜欢nub的用户列表中,如果用户确实喜欢post,则会将其删除
toggleFav的不利功能也不起作用,因此我们也非常感谢您的帮助,但这是一个能够访问faved阵列的正确子级的问题,我不知道该怎么做
我认为当某件事情受到青睐时,页面需要更新正确的信息,但我不确定如何实现它。
/*如何在fire base中存储数据:
{
“家”:{
“房间1”:{
“状态”:“正确”,
“开关名称”:“灯2”,
“用户id”:“-Kvbk-XHqluR-hB8l2Hh”
}
}
}
*/
//选择需要实时数据的元素。
const preObject=document.getElementById('tbl_开关_列表');
//选择根表名称
const dbreObject=firebase.database().ref().child('home');
//在Firebase中更改值并在控制台中查看。
dbreObject.on('value',snap=>console.log('Response:',snap.val());
开关列表
#身份证
#开关名
#地位
非常确定您需要关注更改。或者尝试AngularFire:$firebaseArray。$loaded
仅在加载初始数据时触发一次。对基础数据的任何后续更改都不会触发$loaded
。您可能应该在触发$loaded
时修改数组正在扩展$firebaseArray
。这里有很多格式奇怪的代码……没有任何进一步的解释。我怀疑这是否真的有助于回答手头的问题。
var ref = new Firebase("https://xxxxx.firebaseio.com");
var auth = ref.getAuth();
var nubRef = new Firebase("https://xxxxx.firebaseio.com/Nubs");
var nubs = $firebaseArray(nubRef);
$scope.nubs = nubs;
var userRef = new Firebase("https://xxxxx.firebaseio.com/users");
var users = $firebaseArray(userRef);
$scope.users = users;
// Array of booleans for favorites
$scope.favedArray = [];
// Array of user ids for
$scope.userIdArray = [];
var userFavs = $firebaseArray(userRef.child(auth.uid).child("favorites"));
$scope.userFavs = userFavs;
userFavs.$loaded()
.then
(
function()
{
nubs.$loaded()
.then
(
function()
{
$scope.tempFaved = [];
$scope.tempId = [];
console.log(userFavs);
angular.forEach
(
nubs,
function(nub)
{
$scope.tempFaved.push(false);
$scope.tempId.push(nub.$id);
console.log($scope.tempId);
angular.forEach
(
userFavs,
function(favs)
{
console.log($scope.tempFaved);
if(favs.nub == nub.$id)
{
$scope.tempFaved.pop();
$scope.tempFaved.push(true);
console.log($scope.tempFaved);
}
}
);
}
);
while($scope.tempFaved.length > 0)
{
$scope.favedArray.push($scope.tempFaved.pop());
$scope.userIdArray.push($scope.tempId.pop());
}
$scope.getFaved = function(nubId)
{
console.log($scope.favedArray[$scope.userIdArray.indexOf(nubId)]);
$scope.faved = $scope.favedArray[$scope.userIdArray.indexOf(nubId)];
return $scope.faved;
}
$scope.toggleFav = function(nub)
{
var nubFavRef = nubRef.child(nub.$id).child("favorites");
var nubFavs = $firebaseArray(nubFavRef);
var faved = $scope.getFaved(nub.$id)
console.log(faved);
if (faved == false)
{
nubFavs.$add
(
{
user: auth.uid
}
);
userFavs.$add
(
{
nub: nub.$id
}
)
console.log("favorited");
}
else
{
nubFavs.$remove(auth.uid);
userFavs.$remove(nub.$id);
console.log("unfavorited");
}
};
}
)
}
);