2016-07-07 93 views
4

用戶模式由用戶不喜歡的產品的評論:喜歡和在角

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中的顯示中,圖形成功立即改變,但ng-bind =「review.numoflikes」和ng-bind =「review.numofdislikes」的跨度不會立即更新,而是更新頁面刷新

數據庫正在成功更新

此外,如果您可以幫助我限制喜歡或不喜歡的用戶,它會很好。謝謝。

+0

我不會檢查你的代碼,但只推薦一些檢查:(1)喜歡/不喜歡正確綁定(即使用'NG-模型「內的HTML)的HTML元素? (2)是否與'$ scope'或'$ rootScope'已知的數據庫交互?按照限制喜歡/不喜歡的一種方式,只要用戶選擇一個按鈕即可禁用按鈕。 – FDavidov

回答

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