How to pass data from factory to controller in angular js?
angularjs, javascript
Solution
Various ways, the first one that comes to mind is something like this:
//in your factory
return {
saveCustomer: function(data) {
var request = $http({...});
return request;
}
}
//in your controller
authFactor
.saveCustomer(data)
.success(function() {
//update controller here
})
Problem
I have one factory contains save customer function.On success I want to pass its response in controller so that i can update the view. Factory ``` sampleApp.factory("authFactory", function($location, $http, transformRequestAsFormPost) { return { saveCustomer: function(data) { var request = $http({ method: "post", url: "webservice/ws.php?mode=saveCustomer", transformRequest: transformRequestAsFormPost, data: data }); request.success( function(response) { console.log(response); } ); } }; }); ``` Controller ``` sampleApp.controller('customerController', function($scope, testService,authFactory,$http) { $scope.addCustomer = function() { var data = {name: $scope.customerName,city: $scope.customerCity}; // Calling Factory Function authFactory.saveCustomer(data); // How to fetch response here } }); ``` Please help me to solve that problem Thanks