2016-07-03 18 views
1

控制器:節目glyphicon基於值的html角

.controller('BlogController', function(blogFactory, $routeParams, $scope){ 

    var that=this; 

    stat=false; 

    this.checkbookmark = function(bId){ 
    console.log(bId) 
    blogFactory.checkBookmark(bId, function(response){ 
     console.log(response) 
     if(response == "bookmarked"){ 
      that.stat = true; 
     } 
     else{ 
      that.stat = false; 
     } 
    }) 
} 

HTML代碼:

<div class="container" ng-controller="BlogController as blogCtrl"> 

    <div class="row" ng-repeat="chunk in blogCtrl.blogs | filter: filter_name | orderBy:'-created_at' | groupBy: 3"> 


     <div class="outerbox1 col-sm-4" ng-repeat="blog in chunk" > 
      <div class="innerbox3" ng-init="blogCtrl.checkbookmark(blog._id)"> 
       <br> 
       <div> > READ MORE 
        <a ng-if="blogCtrl.stat" ng-href="#" ng-click="blogCtrl.removebookmark(blog._id)" class="glyphicon glyphicon-heart pull-right">{{blogCtrl.stat}}</a> 
        <a ng-if="!blogCtrl.stat" ng-href="#" ng-click="blogCtrl.addbookmark(blog._id)" class="glyphicon glyphicon-heart-empty pull-right">{{blogCtrl.stat}}</a> 
       </div> 

      </div> 
     </div> 
    </div> 
</div> 

我想基於STAT

的值,以顯示glyphicon我有6個博客,前3個是書籤,後3個不是。

我收到的問題是,統計值總是根據最後一個書籤設置,因此它總是假/真(基於上一次博客的條件)。

如何解決這個問題?

回答

3

而不是設置控制器上的統計特性,你應該將博客對象的屬性(這顯然屬於對象)

這個替換您checkbookmark功能:

this.checkbookmark = function(blog){ //pass the entire blog, not just the id 
    blogFactory.checkBookmark(blog._id, function(response){ 
     console.log(response) 
     if(response == "bookmarked"){ 
      blog.stat = true; //set the property on blog instead of the controller 
     } 
     else{ 
      blog.stat = false; 
     } 
    }) 
} 

然後使用這樣的:

<div class="innerbox3" ng-init="blogCtrl.checkbookmark(blog)"> 
    <br> 
    <div> > READ MORE 
     <a ng-if="blog.stat" ng-href="#" ng-click="blogCtrl.removebookmark(blog._id)" class="glyphicon glyphicon-heart pull-right">{{blog.stat}}</a> 
     <a ng-if="!blog.stat" ng-href="#" ng-click="blogCtrl.addbookmark(blog._id)" class="glyphicon glyphicon-heart-empty pull-right">{{blog.stat}}</a> 
    </div> 
</div> 

您將需要作出類似更改爲您addremovebookmark功能s

+0

它的工作。謝謝......現在您能否告訴我如何實時更新顯示屏,即如果用戶點擊它就會更改圖標圖標。 @ x4rf41 – kRAk3N

+0

正如我所說的,您需要對添加和刪除功能進行相同的更改:傳遞整個博客不僅僅是id,然後將blog.stat屬性設置爲新值 – x4rf41

+0

確定完成,謝謝@ x4rf41 – kRAk3N