2016-07-04 60 views
0

我從「cameraData」服務調用camera.json文件並將cameradata服務注入到「CameraController」中。如果我點擊刷新按鈕後更改camera.json,我正在獲取舊數據。任何想法?更改json文件后角度工廠返回數據保持不變

.factory( 'cameraData',函數($ HTTP,$ Q,全局變量){ VAR推遲= $ 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」,函數($範圍,$超時,cameraData){$ = scope.refreshCameraData函數(){

$scope.allCamera = []; 
    cameraData.GetItemList() 
    .then(function(data) { 
     $scope.allCamera = data.Camera; 
    }, function(err) { 
     // promise rejected, could log the error 
     console.log('error', err); 
    }); 

} 

cameraData.GetItemList() 
    .then(function(data) { 
     $scope.allCamera = data.Camera; 
    }, function(err) { 
     // promise rejected, could log the error 
     console.log('error', err); 
    }); 

})

+0

歡迎SO 。你能不能格式化代碼的第一行和最後一行,因爲這可能有助於人們快速理解代碼,從而更多地提供答案心情;-)? – Dilettant

回答

0

您需要在每次調用工廠方法時創建延遲對象。每次調用api時它都會返回新的promise。用以下方式更改您的工廠代碼。

var cameraData = {}; 
$('.loader').show();   
var contentType = "application/json; charset=utf-8"; 

cameraData.GetItemList = function(){ 
    // need to 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; 
}) 
+0

Succes已棄用,請使用'.then'。此外,'$ http'已經返回一個承諾本身 –

+0

這是所有在一起不同的討論.. :) – dhavalcengg

+0

不同的討論與否,你不應該傳播不推薦/錯誤的代碼 –

1

您不需要顯式創建延遲對象並手動解析/拒絕它。 $http()方法本身返回一個承諾,可以直接返回,如下所示。

.factory('cameraData', function ($http, $q,globalVariable) { 
    var cameraData = {}; 
    $('.loader').show();   
    var contentType = "application/json; charset=utf-8"; 

    cameraData.GetItemList = function(){ 
     $('.loader').show(); 
     var senddata ={}; 
     senddata.installedcameraid = "9547857793457943"; 

     return $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 
      } 

     }).then(function(response){ 
      $('.loader').hide(); 
     }).error(function(error){ 
      $('.loader').hide(); 
     }); 
    } 

    return cameraData; 
}); 

而且,對於隱藏加載器,你可以使用如下的.finally()方法:

 return $http({ 
      url: globalVariable.Camerafilepath, 
      dataType: 'json', 
      method: "POST", 
      data: JSON.stringify(senddata), 
      headers: { 
       "Content-Type": contentType, 
       "access_token": globalVariable.TOKEN 
      } 

     }).finally(function(response){ 
      $('.loader').hide(); 
     }); 

在您的控制器,您現在可以訪問響應的,

.controller('CameraController',function($scope,$timeout,cameraData){ 

    $scope.refreshCameraData = function(){ 

     $scope.allCamera = []; 

     cameraData 
      .GetItemList() 
      .then(function(response) { 
       $scope.allCamera = response.data.Camera; 
      }, function(err) { 
       // promise rejected, could log the error 
       console.log('error', err); 
      }); 

    } 


    cameraData.GetItemList() 
     .then(function(response) { 
      $scope.allCamera = response.data.Camera; 
     }, function(err) { 
      // promise rejected, could log the error 
      console.log('error', err); 
     }); 
    }) 
}) 
+0

您可以使用'.finally'而不是'.success'和'.error','.success'和' .error'已棄用 –

+0

是的。編輯過了嗎?感謝您指點雖然:) –

+0

是的,我可以使用。然後,但我的問題是如何從工廠獲取更改的camera.json數據?不管怎樣,謝謝 – user1955726