Я написал ui.router для получения данных из файла JSON. Я не получаю сообщения об ошибках, но код не входит в "разрешающую" часть. Поэтому я не могу получить данные из файла JSON.
Может ли кто-нибудь сказать мне, что может сделать это, и как это исправить?
Вот мой код. (Функция() {
/*
* Declaration of main angular module for this application.
*
* It is named turtleFacts and has no dependencies (hence the
* empty array as the second argument)
*/
angular
.module('GrammarQuiz', ['ui.router'])
.config(
function($stateProvider, $urlRouterProvider) {
console.log('HOLY SMOKES, I CAN BREATHE IN HERE')
$urlRouterProvider.otherwise('/browse/1');
$stateProvider
.state('home',{
url: "/#/testtest",
resolve: {
questions: function($http){
console.log('!!#@!#@!');
return $http({
method: 'GET',
url: 'api/data1.json'
}).error(function(data,status,headers,config){
console.log('thisi now working!!!!');
})
}
}
})
})
})();
Я уверен, что вы должны вернуть нужное значение из обратных вызовов $ http:
resolve: {
questions: function($http) {
return $http({
method: 'GET',
url: 'api/data1.json'
}).success(function(data) {
return data; // this is what you're missing
}).error(function() {
console.log('error');
});
}
}
Но вы действительно должны использовать. Then вместо.success и.error
resolve: {
questions: function($http) {
return $http({
method: 'GET',
url: 'api/data1.json'
}).then(function success(response) {
return response.data;
}, function error(response) {
console.log('error');
});
}
}
попробуйте изменить вопрос: function ($ http)... to
question: ['$http', fucntion($http) {..}]
function($http) {}
должно работать нормально, поскольку параметр $http
совпадает с фактическим именем службы $ http.