2016-11-20 58 views
1

我写了一个显示帖子列表的离子页面,每个帖子都有一个用于标记它的收藏按钮(星形图标)。当我点击收藏按钮时,数据将被插入到数据库中,但图标不会立即标记。重新加载页面后,它被标记。所以点击它时如何使图标立即标记。制作收藏夹按钮显示标记不带重新加载页面Ionic

这里是我的网页代码,它使用$scope.post_infos显示数据,如果变量“标记”的存在,最喜欢的按钮,将显示星形图标,否则概述星形图标:

<ion-list> //ng-controller is recuitmentCtrl 
<div ng-repeat="post_info in post_infos" > 
    <ion-item> 
    <div class=" item-button-right"> 
      <a ng-click="showContent(post_info.id)"> 
      <div class="post"> 
        <div class= "title">{{post_info.title}}</div> 
        <div class= "description">{{post_info.description}}</div> 
      </div> 
      </a> 
      <button class="button button-icon i" ng-click="isFavorite(post_info.id)"> 
      <i ng-class="{'icon ion-android-star-outline': !post_info.marked, 
         'icon ion-android-star': post_info.marked}"></i> 
      </button> 
    </div> 
    </ion-item> 
</div> 
</ion-list> 

在我controller.js

.controller('recuitmentCtrl', ['$scope', '$state', function ($scope, $state,) { 

    var ref = firebase.database().ref('posts'); 
    var titles, descriptions; 
    $scope.post_infos = []; 

    ref.on('value' ,function(snapshot){ 
     var userID = firebase.auth().currentUser.uid; 
     $scope.post_infos = []; 

     snapshot.forEach(function(childSnapshot){ 

      var marked = firebase.database().ref('users/'+ userID + '/favorites/' + childSnapshot.key); 
      marked.on('value', function(snapshot_user){ 
      var obj = { 
       "id":   childSnapshot.key, 
       "title":  childSnapshot.val().title, 
       "description": childSnapshot.val().description, 
       "marked":  snapshot_user.child("marked").val() 
      }; 
      $scope.post_infos.push(obj); 
     }) 
    }); 
}); 

    $scope.showContent = function(param){ 
     $state.go('postdetail',{postID: param}); 
    }; 

    $scope.isFavorite = function(postID){ 
     var userID = firebase.auth().currentUser.uid; 
     var userRef = firebase.database().ref('users/'+ userID + '/favorites/' + postID); 
     userRef.set({ 
      marked: true 
     }).then(function(result){ 
    }); 
} 

}])

这里是我的数据库:

Firebase database

回答

1

您需要更新变量post_info时,它的标记为收藏,这里有两种方法可以做到这一点:

控制器

$scope.isFavorite = function(post){ 
    var userID = firebase.auth().currentUser.uid; 
    var userRef = firebase.database().ref('users/'+ userID + '/favorites/' + post.id); 
    //post.marked = true; //instant feedback 
    userRef.set({ 
     marked: true 
    }).then(function(result){ 
     //post.marked = true; //will mark as favorite as soon as the server accepts the request 
    }); 

模板

<button class="button button-icon i" ng-click="isFavorite(post_info)"> 
     <i ng-class="{'icon ion-android-star-outline': !post_info.marked, 
        'icon ion-android-star': post_info.marked}"></i> 
</button> 
+0

它像一个魅力。我尝试了两条注释行,两者都起作用,但它也添加了我在列表底部点击的帖子。因此,我将marked.on(“value”)更改为maked.once(“value”)。完善!!。非常感谢!! – ThinhPhan

+0

嗨mcrvaz,我做了你说的和它的工作,但现在当我发布一篇新文章时,我面临着一个关于ref.on('value')的新问题。我想你可以在不看到我的postController代码的情况下理解它是什么,我的postController将发布一篇新文章,点击提交时,它会重定向到上面的页面。这里的问题是列表中的每个帖子显示两次,但是如果我评论*变量标记为*的所有行,它将起作用。也许它与回调有关,我尝试了一些ref.off,但它没有帮助。请问你能帮帮我吗! – ThinhPhan

+0

看来你正在观察'ref.on('value')'的变化,并且每次将值更改为 '$ scope.post_infos.push(obj);'时将新的'post_info'推入列表。 也许你可以在添加列表之前检查'post_info'是否已经存在,或者只是更新列表上的值而不是添加新列表中的值。 – mcrvaz