我有一個簡單的服務,它從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