Answer the question
In order to leave comments, you need to log in
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
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);
}
);
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.
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);
});
perem = $this;
$http.get('data/category.json').success(function(data){
perem = data;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question