用户架构:
var UserSchema = new Schema({
review_likes : [{type:String}],
review_dislikes : [{type:String}]
});
审核架构
var ReviewSchema = new Schema({
productID:{type: String, required: true},
numoflikes:{type:Number, required:true},
numofdislikes:{type:Number, required:true}
})
审核控制器:
.controller('reviewsController', function($route, reviewsFactory, $scope, $routeParams){
var that=this;
reviewid = $routeParams.id;
productID = $routeParams.id;
likestats = false;
dislikestats = false;
/* console.log('controller',questionid)
*/ var getallReviews = function(){
reviewsFactory.getReviews(function(data){
that.reviews = data;
})
}
getallReviews();
var getProReviews = function(productID){
reviewsFactory.getProductReviews(productID, function(data){
that.proreviews = data;
})
}
getProReviews(productID);
$scope.pID=productID;
this.addReview = function(){
reviewsFactory.addReview(this.newReview, function(message){
getallReviews();
console.log(message)
that.messages = message
})
this.newReview = {};
}
var getthatReview = function(){
reviewsFactory.getthatReview(reviewid, function(data){
/* console.log('[CONTROLLER] That Question:',data);
*/ that.review = data;
})
}
getthatReview();
this.getThatReview = function(){
reviewsFactory.getthatReview(reviewid, function(data){
/* console.log('[CONTROLLER] That Question:',data);*/
reviewsFactory.checkRevLike(reviewid, function(response){
if(response == "liked"){
data.likestats = true;
}
else{
data.likestats = false;
}
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.likestats = data.likestats; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.likestats = data.likestats; });
})
that.review = data;
})
}
this.addlikereview = function(rId){
reviewid = rId;
reviewsFactory.addRevLike(rId, function(response){
that.getThatReview();
})
}
this.removelikereview = function(rId){
reviewid = rId;
reviewsFactory.removeRevLike(rId, function(response){
that.getThatReview();
})
}
this.checklikereview = function(review){
reviewid = review._id;
reviewsFactory.checkRevLike(review._id, function(response){
if(response == "liked"){
review.likestats = true;
}
else{
review.likestats = false;
}
})
}
this.getThatReview2 = function(){
reviewsFactory.getthatReview(reviewid, function(data){
/* console.log('[CONTROLLER] That Question:',data);*/
reviewsFactory.checkRevDisLike(reviewid, function(response){
if(response == "disliked"){
data.dislikestats = true;
}
else{
data.dislikestats = false;
}
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.dislikestats = data.dislikestats; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.dislikestats = data.dislikestats; });
})
that.review = data;
})
}
this.adddislikereview = function(rId){
reviewid = rId;
reviewsFactory.addRevDisLike(rId, function(response){
that.getThatReview2();
})
}
this.removedislikereview = function(rId){
reviewid = rId;
reviewsFactory.removeRevDisLike(rId, function(response){
that.getThatReview2();
})
}
this.checkdislikereview = function(review){
reviewid = review._id;
reviewsFactory.checkRevDisLike(review._id, function(response){
if(response == "disliked"){
review.dislikestats = true;
}
else{
review.dislikestats = false;
}
})
}
})
HTML:
<div ng-controller="reviewsController as revCtrl ">
<div ng-repeat="review in revCtrl.proreviews>
<div ng-init="revCtrl.checklikereview(review)"> LIKE
<div ng-if="review.likestats" ng-href="#" ng-click="revCtrl.removelikereview(review._id)" class="glyphicon glyphicon-star ">
</div>
<div ng-if="!review.likestats" ng-href="#" ng-click="revCtrl.addlikereview(review._id)" class="glyphicon glyphicon-star-empty ">
</div>
<span ng-bind="review.numoflikes"></span>
</div>
<div ng-init="revCtrl.checkdislikereview(review)"> DISLIKE
<div ng-if="review.dislikestats" ng-href="#" ng-click="revCtrl.removedislikereview(review._id)" class="glyphicon glyphicon-star ">
</div>
<div ng-if="!review.dislikestats" ng-href="#" ng-click="revCtrl.adddislikereview(review._id)" class="glyphicon glyphicon-star-empty ">
</div>
<span ng-bind="review.numofdislikes"></span>
</div>
</div>
</div>
我试图喜欢/不喜欢问题评论列表中的评论
我想限制1个喜欢/不喜欢1个用户的评论
这里出现的问题是,在html的显示中,glyphicon立即成功改变,但是使用ng-bind =&#34; review.numoflikes&#34;和ng-bind =&#34; review.numofdislikes&#34;不会立即更新,但会在页面刷新时更新
数据库正在成功更新
另外,如果你可以帮我限制用户喜欢或不喜欢,那就太棒了。感谢。
答案 0 :(得分:2)
找到了一个解决方案..只需尝试一次
this.getThatReview = function(){
reviewsFactory.getthatReview(reviewid, function(data){
/* console.log('[CONTROLLER] That Question:',data);*/
reviewsFactory.checkRevLike(reviewid, function(response){
if(response == "liked"){
data.likestats = true;
}
else{
data.likestats = false;
}
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.likestats = data.likestats; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.likestats = data.likestats; });
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.numoflikes = data.numoflikes; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.numoflikes = data.numoflikes; });
})
that.review = data;
})
}
this.getThatReview2 = function(){
reviewsFactory.getthatReview(reviewid, function(data){
/* console.log('[CONTROLLER] That Question:',data);*/
reviewsFactory.checkRevDisLike(reviewid, function(response){
if(response == "disliked"){
data.dislikestats = true;
}
else{
data.dislikestats = false;
}
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.dislikestats = data.dislikestats; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.dislikestats = data.dislikestats; });
angular.forEach(that.proreviews,function(e){ if(e._id == reviewid)e.numofdislikes = data.numofdislikes; });
angular.forEach(that.reviews,function(e){ if(e._id == reviewid)e.numofdislikes = data.numofdislikes; });
})
that.review = data;
})
}