Answer the question
In order to leave comments, you need to log in
Angular. How to return data value after get request?
Good afternoon!
You need to pull out the data value after the server response to the get request and pass it to the controller. The function that executes the request is in the factory, the request is passed to the controller, then to the view. Before that, there was a controller -> view, everything worked. Now does not work. I know - I messed up somewhere, where - I don’t understand, help me figure it out
factory
var app = angular.module('app', []);
app.factory('dataProvider', function ($http) {
var url = 'http://localhost:53182/api/Employees/';
var employeesList;
return {
setEmployees: function (param) {
this.employeesList = param.data;
},
load: function () {
$http.get(url)
.then(
(function success(data) {
this.setEmployees(data);
}),
(function error() {
alert("ошибка при загрузке данных");
})
);
console.log(this.employeesList); //вот это вот должно выводить массив с данными от сервера
}
}
})
Show how you use your factory
app.controller('testCtrl', function ($scope, $http, dataProvider) {
$scope.activeEmployee = dataProvider.activeEmployee;
$scope.newEmployee = dataProvider.newEmployee;
$scope.adding = dataProvider.adding;
$scope.employees = dataProvider.employees;
$scope.load = function () {
return dataProvider.load();
}
$scope.edit = function (employee) {
return dataProvider.edit(employee);
}
$scope.update = function (employee) {
return dataProvider.update(employee);
}
$scope.add = function (employee) {
return dataProvider.add(employee);
}
$scope.save = function (newEmployee) {
return dataProvider.add(newEmployee);
}
$scope.cancel = function () {
return dataProvider.cancel();
}
$scope.delete = function (employee) {
return dataProvider.delete(employee);
}
$scope.load();
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question