like and dislike review of a product by user in angular

﹥>﹥吖頭↗ 提交于 2019-12-06 04:38:49

问题


user schema:

var UserSchema = new Schema({
    review_likes :      [{type:String}],
    review_dislikes :   [{type:String}]
});

review schema:

var ReviewSchema = new Schema({
    productID:{type: String, required: true},
    numoflikes:{type:Number, required:true},
    numofdislikes:{type:Number, required:true}
})

review controller:

       .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>  

i'm trying to like/dislike a review in a list of reviews in questions

i want to limit 1 like/dislike to a review by 1 user

the problem occurring here is that in the display in html, the glyphicon successfully changes instantly but the span with ng-bind="review.numoflikes" and ng-bind="review.numofdislikes" is not instantly being updated but is updated on page refresh

the database is being successfully updated

also, if you could help me to restrict only like or dislike by user, it would be great. thanks.


回答1:


found a solution .. just try it once

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;
        })
    }


来源:https://stackoverflow.com/questions/38238401/like-and-dislike-review-of-a-product-by-user-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!