Answer the question
In order to leave comments, you need to log in
How to implement asynchronous data loading in AngularJS template?
Hello.
I can't implement data loading using ajax in an AngularJS template.
I am doing on the basis of this example jsfiddle.net/derkoe/Wk7WD/presentation
Only, my data should be loaded into the list.html
app.js template
var app = angular.module('app', []).config(function ($routeProvider) {
$routeProvider.when('/list', {
templateUrl: 'views/list.html',
controller: 'ListCtrl'
, resolve: {
load: function ($route, dataService) {
return dataService.load();
}
}
}).otherwise({
redirectTo: '/list'
});
});
app.controller('ListCtrl', function ($scope, $location, dataService) {
$scope.data = dataService.data;
consol.log(dataService.data);
$scope.back = function () {
$location.path('/list');
};
});
app.factory('dataService', function ($q) {
return {
data : {},
load : function() {
var defer = $q.defer();
var data = this.data;
/*
код из примера
$timeout(function () {
data.id = id;
defer.resolve(data);
}, 1000);
*/
// -- здесь пока обычный jquery-ajax-запрос
// -- знаю что в AngularJS есть $http , но пока не научился им пользоваться
// -- если знаете, покажите с использование $http
$.ajax({
type: 'GET',
url: 'backend/?route=notes:list',
success: function(response) {
$scope.notes = response.data.notes;
console.log(response);
data.notes = response.data.notes;
defer.resolve(data);
},
error: function (req_obj, msg, error) {
console.log('Ошибка запроса');
},
dataType: 'json'
});
//----------------
});
return defer.promise;
}
};
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>AngularJS</title>
</head>
<body ng-app="app">
<div ng-view></div>
<script src="lib/jquery-1.10.2.js"></script>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-resource.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Тестовый список {{test}}
Answer the question
In order to leave comments, you need to log in
1. In addition to $http, you can also use $resource
Your example
$http.get('backend/?route=notes:list').success(function (data) {
...
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question