0
我試圖構建我的第一個角度應用程序,當我試圖將我的工廠保存到服務中時,我不斷收到一個錯誤,該回調不是函數,因此我可以將其用於應用程序的生命。TypeError:回調不是AngularJS中的函數應用
這裏是我迄今爲止
angular.module('games', ['ui.router', 'ui.bootstrap'])
.config(function($urlRouterProvider, $locationProvider, $stateProvider) {
// For any unmatched url, redirect to /state1
$urlRouterProvider.otherwise("/");
//take out #
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
// Now set up the states
$stateProvider
.state('games', {
url: "/",
templateUrl: "/static/app/list.html",
controller: 'gamesCtrl'
})
$stateProvider
.state('game', {
url: "/games/:title",
templateUrl: "/static/app/page.html",
controller: 'pageCtrl'
})
})
.controller('gamesCtrl', ['$scope', '$state', 'gamesService',
function($scope, $state, gamesService) {
$scope.$state = $state;
$scope.games = null;
function init() {
gamesService.getGames().success(function(games) {
$scope.games = games.data;
console.log($scope.games.data)
});
}
init();
}
])
.service('gamesService', ['gamesFactory',
function(gamesFactory) {
//grab the list of games on load
var gamesList = [];
gamesFactory.getGames().success(function(games) {
gamesList = games
});
this.getGames = function(callback){
callback(gamesList);
}
}
])
.factory('gamesFactory', function($http) {
var factory = {};
factory.getGames = function() {
return $http.get('/games.json');
};
return factory;
});
我的錯誤是在這裏
.service('gamesService', ['gamesFactory',
function(gamesFactory) {
//grab the list of games on load
var gamesList = [];
gamesFactory.getGames().success(function(games) {
gamesList = games
});
this.getGames = function(callback){
callback(gamesList);
}
}
])
存在的任何幫助將不勝感激。
只要可能,您應該考慮使用'.then()'而不是'.success()'加上'$ http'。 – Claies