Answer the question
In order to leave comments, you need to log in
How to pass data from factory to controller?
Tell me, how can I transfer the data received via get from the factory, so that later it can be processed in the controller?
var app = angular.module('app',[]);
app.factory('loadData', ['$http', function($http) {
return function() {
$http.get('mydata.json').then(
function(response) {
// вот здесь как возвратить данные?
return response
},
function(data) {
// и здесь
return data
});
// или их нужно возвращать здесь??
};
}]);
app.controller('loadCtrl',['$scope', 'loadData', function($scope, loadData) {
$scope.info = loadData();
// а здесь я хочу обработать info
}]);
Answer the question
In order to leave comments, you need to log in
var app = angular.module('app',[]);
app.factory('serviceData', ['$http', function($http) {
var self = this;
self.loadData = function() {
var promise = $http({
url: 'mydata.json',
method: 'GET',
params: {},
headers: {}
})
.then(function (response) {
var response_data = angular.fromJson(response.data);
return response_data;
});
return promise;
};
return self;
}]);
app.controller('loadCtrl',['$scope', 'serviceData', function($scope, serviceData) {
// а здесь я хочу обработать info
serviceData.loadData()
.then(function (response_data) {
$scope.info = response_data;
});;
}]);
via get data from the factory
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question