I'm having trouble keeping 2 controllers in sync when using my factory/service.
I have (slightly minified)
angular.factory('API', ['$http', function($http){
let self = this;
self.scans = [];
self.updateHistory = function (cb) {
return $http.get('/scanner/history').then(function(scans){
self.scans = scans.data;
if (cb) {
return cb(self.scans);
} else {
return self.scans;
}
}, function(err){
console.error(err);
});
};
return self;
}]);
Then my controllers
angular.controller('NewCrawl', ['$scope', 'API', function ($scope, API) {
$scope.scan = {
url: null
};
$scope.startScan = function () {
if ($scope.scan.url) {
API.start($scope.scan.url)
.then(function(){
API.updateHistory();
});
}
};
}])
and
angular.controller('ScanHistory', ['$scope', 'API', function($scope, API){
$scope.scans = API.scans;
API.updateHistory();
}])
I then have a ngRepeat within my ScanHistory controller like
<tbody>
<tr ng-repeat="scan in scans | orderBy:'id':true">
<td><% scan.id %></td>
<td><% scan.url %></td>
<td><% scan.started %></td>
<td><% finishedText(scan) %></td>
<td><% scan.errors.length %></td>
</tr>
</tbody>
So thats basically the setup, the problem is - when I run API.updateHistory()
it fires, and if i console.log(self.scans)'
within the service i get the expected result.
However my ngRepeat does not update with the newly added item, even though the API.updateHistory
method returned the correct results.
Why!?