我有一个简单的服务,它从HTTP端点抓取数据将其发送回控制器。AngularJS未能注入服务
我的服务还implemnted缓存不过,我就在这行代码此错误TypeError: undefined is not a function
在我的控制器
myappApi.getItems().then(function(data)
我试图弄清楚为什么我不能。 这里是控制器代码:
.controller('ItemsCtrl',['$scope','myappApi',function($scope, myappApi){
myappApi.getItems().then(function(data){
$scope.items = data;
});
}])
正如我在这里使用Ioniframework我如何在app.js
注入我的服务:
angular.module('myApp', ['ionic', 'myApp.controllers', 'myApp.services', 'angular-data.DSCacheFactory'])
,这里是我的服务的代码:
(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
};
};
})();
谢谢你的时间。
你'vascularApi',未在任何地方定义的我可以看到 – Ronnie 2015-02-24 21:47:09
我编辑我的问题这不是真的问题。 – 2015-02-24 21:49:47