2016-01-01 46 views
0

所以我是新的角JS,我遇到了編輯數組中的一個對象的問題。下面的函數被調用,並且正在傳遞適當的對象。循環甚至找到正確的對象並將其喜歡的值設置爲true。然而由於某種原因,當我第二次點擊喜歡的按鈕時,它仍然顯示喜歡的值爲false ...我確定它的東西超級簡單。數組沒有變化

<div class="thelist" data-ng-repeat="b in data| orderBy:choice"> 
        <h2>{{ b.from }}</h2> 
        <p>{{b.date}}</p> 
        <img class="full-image" src="{{b.img}}"> 
        <p>{{b.content}}</p> 
        <p>{{b.likes}}</p> 
        <button class="tab-item" ng-click="liked({{b}})"></button> 

      </div> 

App.js:

ref.on("child_added", function (snapshot) { 
       var newPost = snapshot.val(); 
       $scope.data.push({ 

        content: newPost.content, 
        date: newPost.date, 
        from: newPost.from, 
        img: newPost.img, 
        likes: newPost.likes, 
        realdate: newPost.realdate, 
        id: snapshot.key(), 
        liked: false 

       }); 

      }); 

的很喜歡這種功能:

 $scope.liked = function (post) { 

     if (post.liked == false){ 
        console.log("liked has been run"); 
        angular.forEach($scope.data, function (object) { 

        if (object.id == post.id) { 

         object.liked = true; 

        } 

       }); 

    //add 1 like on server here   

      } 

    } 

回答

2

你不能在ng-click使用模板。更改

<button class="tab-item" ng-click="liked({{b}})"></button> 

<button class="tab-item" ng-click="liked(b)"></button> 
+0

非常感謝您!我知道這會很簡單! – ObjectiveCsam

0

要在@ hege_hegedus的回答擴大, 「NG單擊」 指令需要一個表達式(可以訪問$範圍變量的JavaScript表達式)。由於,因爲它需要一個表達,有$範圍的完全訪問權限,你能想到的

<button class="tab-item" ng-click="liked(b)"></button> 

<button class="tab-item" ng-click="$scope.liked($scope.b)"></button>