angularjs - Updating View in Angular js on ajax success callback -
i working on project getting list of offers service. each offer active or inactive. displying active offers on view on tabular format giving ajax call , using ng-repeat.
when click on link inactivate offer giving ajax request inactivate offer in database. once inactivate offer should not displyed on view. code fect offer , inactivate offer :
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 fect offers , inactivate offer :
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 getting inactivated in response of $scope.inactivate method perticular offer still visible in view.
so how display on active offers once inactivate offer using service call ?
your code correctly performs get
request inactivate offer, not "tell" angular offer has been inactivated. need remove offer offers list $scope.offers
once offer inactivated (ie. when inactivateoffer
promise resolved). try this:
$scope.inactivate = function (id) { mosservicefactory.inactivateoffer(id).then(function (data) { (var = 0; < $scope.offers.length; i++) { if ($scope.offers[i].id === id) { $scope.offers.splice(i, 1); } }); }
Comments
Post a Comment