所以我是新的角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
}
}
非常感謝您!我知道這會很簡單! – ObjectiveCsam