I'm trying to learn AngularJS and there is this thing that I don't understand, which seems like all the internet solved by using $scope.$apply
, but I already use it and it does nothing.
Basically, I use Twitter API to retrieve a timeline, and when we scroll from the bottom, it loads more tweets. This part works, I'm using a factory to do it, but I can display the object receive in the console, I don't have issues here.
I have a view like this, to display the data:
<div class='timeline' ng-controller='TimelineCtrl' is-scrolled='loadData()'>
<div class='tweet' ng-repeat='p in posts'>
<img class='portrait' src='{{p.user.profile_image_url}}' />
<p>{{p.text}}</p>
<p class='date'>{{p.created_at}}</p>
</div>
</div>
My controller looks like this:
$scope.posts = [];
// Load the original tweets list
TwitterAPI.timeline($scope.count, function(data) {
$scope.$apply(function() {
$scope.maxId = data[data.length-1].id;
$scope.sinceId = data[0].id;
$scope.posts.push(data);
});
});
data is legit.
The thing I don't understand at all, and make me think that it's something very easy to solve and I just don't see it, is that if I use '= data' instead of 'push(data)' the view is updated. Even when I load more tweets, if I use '=' the view is updated (with the content being replaced of course which is not what I want).
Note: maxId, sinceId and count are initialized earlier, I didn't put it there since I don't think it matters.