简体   繁体   中英

AngularJS fail to inject a service

I have a simple service which grab data from HTTP end point send it back to controller.

I also implemnted caching in the service however, i get this error TypeError: undefined is not a function on this line of code in my controller

myappApi.getItems().then(function(data)

I tried to figure out why i couldn't. here is the controller code:

.controller('ItemsCtrl',['$scope','myappApi',function($scope, myappApi){
    myappApi.getItems().then(function(data){
        $scope.items = data;
    });
}])

As am using Ioniframework here how i injected my services in the app.js :

angular.module('myApp', ['ionic', 'myApp.controllers', 'myApp.services', 'angular-data.DSCacheFactory'])

and here is the code of my service:

(function() {
    'use strict';

    angular.module('myApp.services',[]).factory('myappApi', ['$http', '$q', '$ionicLoading', 'DSCacheFactory', myappApi]);

    function myappApi($http, $q, $ionicLoading, DSCacheFactory) {


        self.itemsCache = DSCacheFactory.get("itemsCache");

        //to re-use expired cached data if no internet connection
        self.itemsCache.setOptions({
            onExpire: function (key, value) {
                getItems()
                    .then(function () {
                        console.log("items items Cache was automatically refreshed.", new Date());
                    }, function () {
                        console.log("Error getting data. Putting expired item back in the cache.", new Date());
                        self.itemsCache.put(key, value);
                    });
            }
        });

        function getItems() {
            var deferred = $q.defer(),
                cacheKey = "items",
                itemsData = self.itemsCache.get(cacheKey);

            if (itemsData) {
                console.log("Found data inside cache", itemsData);
                deferred.resolve(itemsData);
            } else {
                $http.get("services/data.json")
                    .success(function(data) {
                        console.log("Received data via HTTP");
                        self.itemsCache.put(cacheKey, data);
                        deferred.resolve(data);
                    })
                    .error(function() {
                        console.log("Error while making HTTP call.");
                        deferred.reject();
                    });
            }
            return deferred.promise;
        }

        return {
            getItems: getItems
        };
    };
})();

Thank you for your time.

Take a look in the angular-cache file CHANGELOG.md : "- Angular module renamed to angular-cache - DSCacheFactory renamed to CacheFactory "

You will have to change:

  1. app.js: instead of 'angular-data.DSCacheFactory' use 'angular-cache'
  2. service.js instead of 'DSCacheFactory' use 'CacheFactory'

It looks like you've declared the myappApi factory before the myappApi function is actually defined. Try something like:

angular.module('myApp.services',[]).factory('myappApi', ['$http', '$q', '$ionicLoading', 'DSCacheFactory', 
function($http, $q, $ionicLoading, DSCacheFactory) {
  // myappApi code
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM