javascript - Managing an Array of Likes in AngularJS Service -
i have button on profile page, on click of button want maintain array of , store db.
in profile controller have
$scope.likeprofile = userservice.likeprofile(loggedinuser,$state.params.userid);
in user service have
function likeprofile(likedby,id){ var likearray = []; likearray.push(likedby); ref.child("users").child(id).update({ likedby: likearray}); }
i want understand how not intialize likearray everytime likeprofile method called. likes pushed array.
i this, not sure though if want achieve. initialization should not done within function if want keep result. keeps result within scope of one user.
$scope.likearray = []; function likeprofile (likedby, id) { $scope.likearray.push(likedby); ref.child("users").child(id).update({ likedby: likearray}); }
otherwise, if need overall likes have initialize array previous values within function, sth.
likearray = getlikesfromdb(); // access db likearray.push(likedby); ref.child("users").child(id).update({ likedby: likearray });
try debug browser dev tools (e.g. in chrome tools > developer tools) , see send in network tab. if array contains 1 value whole array overwritten unless write custom update function adds value rather replaces value.
Comments
Post a Comment