E
E
e_pyatin2016-12-10 16:00:38
Angular
e_pyatin, 2016-12-10 16:00:38

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

controller:
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();
  
});

I'm reworking the view, I'm still sitting

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2016-12-10
@Casufi

www.codelord.net/2016/11/23/spotting-outdated-angu...
There is also https://docs.angularjs.org/api/ngResource/service/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question