I am working on a project where I am getting list of the offers from a service. Each offer is Active or Inactive. I am displying only Active offers on a view on tabular format by giving ajax call and using ng-repeat.
When I click on the link Inactivate Offer then I am giving another ajax request to inactivate that offer in database. So once I inactivate offer it should not be displyed on view. My code to fect offer and Inactivate the offer is :
mPosServices.factory('mosServiceFactory',function($http,$rootScope){
return{
viewAllOffers:function(){
var allOffers = $http({
method: "get",
url: "http://myServiceUrl/omnichannel/merchant/offer/view/all?enrollmentId="+$rootScope.enrollMentId,
});
return allOffers;
},
inActivateOffer : function(id){
var inactivate = $http({
method:'get',
url : "http://myServiceUrl/omnichannel/merchant/offer/"+id+"/status/INACTIVE?enrollmentId="+$rootScope.enrollMentId,
});
return inactivate;
}
}
});
and controller code is to fect the offers and inactivate offer is :
var mPosController = angular.module('mPosController', []);
mPosController.controller('offerController', ['$scope', '$rootScope', 'mosServiceFactory', 'ngDialog', function ($scope, $rootScope, mosServiceFactory, ngDialog) {
mosServiceFactory.viewAllOffers().then(function (data) {
$scope.offers = data.data.offers;
console.log($scope.offers);
});
$scope.inActivate = function (id) {
mosServiceFactory.inActivateOffer(id).then(function (data) {
console.log(data);
});
}
}]);
Offer is getting successfully inactivated in response of $scope.inActivate method but that perticular offer is still visible in view.
So how to display on Active offers once I inactivate a offer using service call ?
$scope.offers = data.data.offersin a$scope.$applyAsync(function () { });because you are outside of the digest cycle - which means angular can't pick up the change! Or just call$scope.$apply()after that code