【问题标题】:Empty array from factory method in angularjsangularjs中工厂方法中的空数组
【发布时间】:2016-11-04 15:59:05
【问题描述】:

场景:我有一个工厂服务,代码如下

appServices.factory('MyDB', function ($cordovaSQLite)
{      
var FixedCache = []; 

return {

    getFromCache: function (dataURL, $scope)
    {
        var query = "SELECT * FROM FixedCache WHERE URI_ID = '"+dataURL+"'";
        FixedCache = [];    
        $scope.dataFromCache = [];        

        //console.log("Inside Cache method");
        // Execute query statement from query variable.
        $cordovaSQLite.execute(db, query).then(function (res)
        {                
            if (res.rows.length > 0)
            {


                        var dataItem = {
                            id          : res.rows.item(0).id           ,
                            URI_ID      : res.rows.item(0).URI_ID    ,
                            ExpireDate  : res.rows.item(0).ExpireDate     ,
                            JSONData    : res.rows.item(0).JSONData                            
                        };                            

                        FixedCache.push(dataItem);  
                        $scope.dataFromCache = FixedCache;                            


            }



        }); 

        return FixedCache;

    },// End select all data.        
};

})

在控制器中,我想通过调用这个方法来获取数据。这是控制器的代码。

appControllers.controller('categoriesCtrl', function ($scope, $http, $state, $timeout, $stateParams, $ionicHistory, MyDB, UtilityMethods) {

var dataURL = window.globalVariable.constants.URL_API;

$scope.initialForm = function () {


    $scope.categoriesFromCache = [];
    $scope.categoryCache;


    $scope.url = window.globalVariable.constants.URL_API;
    $scope.todaysDate = UtilityMethods.GetDate(0);

    // $scope.isLoading is the variable use for loading.
    $scope.isLoading = false;           

    $scope.apiUrl = window.globalVariable.constants.URL_API;


};// End initialForm.


$scope.GetFromCache = function (uri, $scope) {


    $scope.dataFromCache = [];        

    MyDB.getFromCache(uri, $scope);              
};

$scope.navigateTo = function (targetPage, objectData, apiUrl) {

    $state.go(targetPage, {
        category: objectData,
        apiUrl: apiUrl
    });
};



$scope.clearSearch = function() {
    $scope.searchByName = '';
};


$scope.goBack = function () {
    $ionicHistory.nextViewOptions({
        disableBack: true
    });
    $state.go("app.categories", {
        isShowError: false
    });
};

$scope.initialForm();


   $timeout(function () {            
        $scope.GetFromCache(dataURL, $scope);


    }, 3000);// End loading progress.

});

问题: 当我调用这个方法 MyDB.getFromCache(uri, $scope);在控制器中,它返回空数组 [] 数据库确实包含数据。

请指导我。我是否正确调用函数?

【问题讨论】:

    标签: javascript angularjs sqlite ionic-framework factory


    【解决方案1】:

    您的代码无法正常工作的原因是,您正在考虑异步事件以同步方式工作,但另外我想说的是,将 $scope 作为参数传递给工厂方法并不被认为是一种好的做法。

    基本上你需要做的是从方法和链中返回cordova promise,该方法和链承诺在控制器中并在其中设置范围变量。

    工厂

    getFromCache: function(dataURL) {
      var query = "SELECT * FROM FixedCache WHERE URI_ID = '" + dataURL + "'";
      FixedCache = [];
      //returning cordova promise
      return $cordovaSQLite.execute(db, query).then(function(res) {
        if (res.rows.length > 0) {
          var dataItem = {
            id: res.rows.item(0).id,
            URI_ID: res.rows.item(0).URI_ID,
            ExpireDate: res.rows.item(0).ExpireDate,
            JSONData: res.rows.item(0).JSONData
          };
    
          FixedCache.push(dataItem);
          return FixedCache; //return data to chained promise
        }
      });
    }
    

    控制器

    $scope.GetFromCache(dataURL).then(function(data){                
         $scope.dataFromCache = data;
    });
    

    【讨论】:

    • @Abubaker 很高兴为您提供帮助,谢谢 :-)
    • 我真的很高兴我得到了答案。我是 Angularjs 的新手。在从工厂提取数据时仍然卡在加载程序中:) 正在处理它。
    • 这里的loader是什么意思?
    • 从数据库加载数据时,至少需要一秒钟左右的时间。此外,当通过http请求获取数据时,需要几秒钟,因此需要在请求期间显示加载器。
    • 我还有另一个问题有点跑题了。你们是如何收到在 Stack Overflow 上发布问题的通知的。我很快就收到了您的回复。
    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多