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