I'm a beginner with Angular and I feel like I'm struggling to grasp the digest cycle. I'm attempting to update a badge count within an ion-tab (using Ionic).
"ion-tab"
<ion-tab title="Requests" badge="data.badge" ng-controller="RequestTabCtrl" badge-style="badge-assertive" icon-off="ion-pull-request" icon-on="ion-pull-request" href="#/tab/requests">
<ion-nav-view name="tab-requests"></ion-nav-view>
I've created a factory that stores an array, which gets updated via socket.io.
"notifications factory"
.factory('notifications',function(){
var list = [];
return{
all: function(){
return list;
},
add: function(data){
list.push(data);
},
length: function(){
return list.length;
}
};
});
.controller('RequestTabCtrl',function($scope,notifications){
$scope.data = {
badge : notifications.length()
};
});
The issue I'm facing is that the badge count doesn't update when the notifications array changes through socket.io updates. I have confirmed that the array is being appropriately updated. I can even log the array length and see it changing. Additionally, I've set a scope variable in the ion-tab's child io-nav-view, so I can observe the expression {{requests.length}} updating in that view.
.controller('RequestsCtrl', function($scope,notifications) {
$scope.requests = notifications.all();
})
I've attempted using $watch (within RequestTabCtrl) on notifications.length. I've also tried calling $apply (within RequestTabCtrl), resulting in a "$digest already in progress" error. Furthermore, I experimented with $timeout without seeing any positive outcome (both in RequestTabCtrl and the factory length function). Any help would be greatly appreciated.