D
D
Dmitry Chuprina2016-02-17 10:53:48
JavaScript
Dmitry Chuprina, 2016-02-17 10:53:48

Angular js How to get data to further work with array further in controller?

$http.get('data/category.json').success(function(data){
$scope.category = data
});
$scope.category - outside the success function, the console prints undefined

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nicholas, 2016-02-17
@mufenman

1. success deprecated, use Promises.
Example:

var promise = $http.get('data/category.json');
promise
.then(
    function(data){
        $scope.category = data
    }, 
    function(error) {
        console.log(error);
    }
);

If you want to do something with an array, you can do it in a .then statement.
for example
Well, in general, read what the Promise API is, for example here: Promises API

S
sasha, 2016-02-17
@madmages

yes, because the request goes asynchronously. outside the succsess function, this data is not yet available, but inside this function, it comes from the server.

N
Nikita Baev, 2016-02-17
@drugoi

Because it is asynchronous (success).
Call a function that will work with the data from success and pass data there.
Example:

var workIngWithData = function(dataObj) {
  console.info(dataObj);
  // Здесь можно работать с data
};
$http.get('data/category.json').success(function(data) {
  $scope.category = data;
  workIngWithData(data);
});

S
Stanislav Pochepko, 2016-02-17
@DJZT

perem = $this;
$http.get('data/category.json').success(function(data){
perem = data;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question