2015-08-27 45 views
0

我正在構建一種社交網絡,我有一個角度指令,點擊後會跟隨一個人。按鈕將出現在該人員的帖子旁邊。所以如果一個人張貼3次按鈕就會重複3次。但是當我點擊一個按鈕時,另外兩個不顯示相同的結果。他們保持不變。角度同步指令?

所以,問題是我如何可以同步其他2個按鈕?

這是我的代碼如何工作。這是一個用戶的帖子,它在右下角 enter image description here

類似的按鈕,這是當我點擊後,就會有模態彈出顯示有關的帖子更詳細當你點擊只喜歡在Facebook中的圖像。

enter image description here

那麼問題是,當我在模態彈出點擊拇指不會在原來的 enter image description here

這用拇指同步上漲的投票代碼

VoteButtons.link = function (scope, ele, att) { 
var post_id = att.voteButtons; 
scope.up_vote_class = 'default'; 
scope.down_vote_class = 'default'; 
scope.up_votes_number = 0; 
scope.down_votes_number = 0; 
scope.post_id = post_id 
var up_votes_id = []; 
var down_votes_id = []; 
if (post_id) { 
    $http.get('/api/posts/get-votes/' + post_id).success(function (res) { 
    if (res && !res.error) { 
     up_votes_id = res.response.up_votes; 
     down_votes_id = res.response.down_votes; 
     if (up_votes_id) { 
     scope.up_votes_number = up_votes_id.length; 
     } 

     if (down_votes_id) { 
     scope.down_votes_number = -down_votes_id.length; 
     } 

     if ($rootScope.user && up_votes_id && up_votes_id.indexOf($rootScope.user._id) != -1) { 
     scope.up_vote_class = 'success'; 
     } 

     if ($rootScope.user && down_votes_id && down_votes_id.indexOf($rootScope.user._id) != -1) { 
     scope.down_vote_class = 'danger'; 
     } 

    } 
    }) 

    scope.thumb_up = function() { 
    $http.post('/api/posts/up-vote/' + post_id).success(function (res) { 
     if (res && !res.error) { 
     up_votes_id = res.response.up_votes; 
     down_votes_id = res.response.down_votes; 
     if (up_votes_id) { 
      scope.up_votes_number = up_votes_id.length; 
     } 

     if (down_votes_id) { 
      scope.down_votes_number = -down_votes_id.length; 
     } 

     if ($rootScope.user && up_votes_id && up_votes_id.indexOf($rootScope.user._id) != -1) { 
      scope.up_vote_class = 'success'; 
      Socket.emit('thumb_up',res.response) 
     } else { 
      scope.up_vote_class = 'default'; 
     } 

     if ($rootScope.user && down_votes_id && down_votes_id.indexOf($rootScope.user._id) != -1) { 
      scope.down_vote_class = 'danger'; 
     } else { 
      scope.down_vote_class = 'default'; 
     } 

     } else { 
     $rootScope.alerts = res.error.message; 
     } 
    }) 
    } 

    scope.thumb_down = function() { 
    $http.post('/api/posts/down-vote/' + post_id).success(function (res) { 
     if (res && !res.error) { 
     up_votes_id = res.response.up_votes; 
     down_votes_id = res.response.down_votes; 
     if (up_votes_id) { 
      scope.up_votes_number = up_votes_id.length; 
     } 

     if (down_votes_id) { 
      scope.down_votes_number = -down_votes_id.length; 
     } 

     if ($rootScope.user && up_votes_id && up_votes_id.indexOf($rootScope.user._id) != -1) { 
      scope.up_vote_class = 'success'; 
     } else { 
      scope.up_vote_class = 'default'; 
     } 

     if ($rootScope.user && down_votes_id && down_votes_id.indexOf($rootScope.user._id) != -1) { 
      scope.down_vote_class = 'danger'; 
     } else { 
      scope.down_vote_class = 'default'; 
     } 
     } else { 
     $rootScope.alerts = res.error.message; 
     } 
    }) 
    } 
} 
} 
+0

我不知道,如果這個問題是相關的在這裏,但也......每個配置文件必須有追隨者的列表,當你點擊「關注」,更新使用新的名稱/ ID該列表,使用範圍對象管理指令。 您的問題沒有代碼或細節,我的答案也沒有。 –

+0

請提供您的代碼,並更具體地說明點擊按鈕時您期望的行爲/結果。 – ArBro

回答

1

要將更改傳達給所有其他指令實例,您可以使用發佈/訂閱樣式。這可以通過使用$rootScope.$broadcast廣播事件來完成,該事件可能通過正在遵循的新事物的iD。

事件然後可以聽了所有的指令並在回調檢查指令是否與被跟蹤的那個東西。如果是,請更新按鈕的狀態。

很難解釋,但我希望你能理解我的做法的邏輯。

而且,希望它可以幫助你!