angularjs - angular factory return data remained same after changing the json file -
i calling camera.json file "cameradata" service , injecting cameradata service in "cameracontroller". if change camera.json after clicked on refresh button getting old data. idea?
.factory('cameradata', function ($http, $q,globalvariable) { var deferred = $q.defer();
var cameradata = {}; var contenttype = "application/json; charset=utf-8"; cameradata.getitemlist = function(){ $('.loader').show(); var senddata ={}; senddata.installedcameraid = "9547857793457943"; $http({ //url: globalvariable.serveraddress + "admin_getcamerapoints", url: globalvariable.camerafilepath, datatype: 'json', method: "post", data: json.stringify(senddata), headers: { "content-type": contenttype, "access_token": globalvariable.token } }).success(function(response){ //$scope.response = response; deferred.resolve(response); return deferred.promise; }).error(function(error){ //$scope.error = error; deferred.reject(error); }); return deferred.promise; } return cameradata;
})
.controller('cameracontroller',function($scope,$timeout,cameradata){ $scope.refreshcameradata = function(){
$scope.allcamera = []; cameradata.getitemlist() .then(function(data) { $scope.allcamera = data.camera; }, function(err) { // promise rejected, log error console.log('error', err); }); } cameradata.getitemlist() .then(function(data) { $scope.allcamera = data.camera; }, function(err) { // promise rejected, log error console.log('error', err); });
})
you need create defer object everytime call factory method. should return new promise everytime call api. change factory code following.
var cameradata = {}; $('.loader').show(); var contenttype = "application/json; charset=utf-8"; cameradata.getitemlist = function(){ // need create defer object everytime var deferred = $q.defer(); $('.loader').show(); var senddata ={}; senddata.installedcameraid = "9547857793457943"; $http({ //url: globalvariable.serveraddress + "admin_getcamerapoints", url: globalvariable.camerafilepath, datatype: 'json', method: "post", data: json.stringify(senddata), headers: { "content-type": contenttype, "access_token": globalvariable.token } }).success(function(response){ //$scope.response = response; $('.loader').hide(); deferred.resolve(response); return deferred.promise; }).error(function(error){ //$scope.error = error; $('.loader').hide(); deferred.reject(error); }); return deferred.promise; } return cameradata; })
Comments
Post a Comment