Im trying to set a counter for the number of alerts that a user has. If I console.log the $scope.alerts and $scope.count, I can see that both are updating, but using {{alerts.length}} or {{count}} as the value of #msgCount will only give me the initial count on pageLoad, and doesn't update when the length of the array changes. What am I doing wrong?
My controller:
myApp.controller('globalAlerts', ['$scope', '$http', function ($scope, $http) {
$scope.alerts = [{
"type": "warning",
"msg": "This is a generic alert. It is best suited for one or two line messages."
},
{
"type": "danger",
"msg": "<strong>Past Due!</strong> You have an invoice <a class=\"invoice-link\" href=\"\">192607</a> that is past due. <a href=\"invoices.html\">View all invoices.</a>"
},
{
"type": "success",
"msg": "This green alert indicates something positive occurred. Doesn't that make you feel happy?"
},
{
"type": "info",
"msg": "This blue alert is another generic warning usually used to inform. Isn't it soothing?"
}
];
$scope.$watchCollection('alerts.length', function() {
return $scope.alerts.length;
}, true);
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
$scope.dynamicPopover = {
templateUrl: 'partials/alerts/globalAlerts.html',
title: 'Recent Alerts'
};
}]);
And the markup:
<div id="upperNav" ng-controller = "globalAlerts">
<a class="btn btn-link" id="msg" popover-placement="bottom" uib-popover-template="dynamicPopover.templateUrl" popover-title="{{dynamicPopover.title}}" >
<div class="badge badge-warning" id="msgCount">{{alerts.length}}</div>
ALERTS</a></div>
With the popover markup:
<div id="alertCntr" ng-controller = "globalAlerts">
<uib-alert ng-show="alerts.length" ng-repeat="alert in alerts track by $index" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<p ng-hide="alerts.length">You ain't got no more alerts, kid!</p>
</div>