Reputation: 791
I'm trying to move this http request to a service and because I'm new to AngularJS I'm having some real issues. Can you help me figure out how to move not only the http request but the viewValue over from the controller to the service?
app.controller('poController',
function poController($scope, $http, poService) {
$scope.test = '';
$scope.fetchPartNumbers = function ($viewValue) {
return $http.get('/api/GetInventoryById/' + $viewValue
).then(function (res) {
var PartNumbers = [];
angular.forEach(res.data, function (item) {
PartNumbers.push(item.PartNumber);
});
return PartNumbers;
});
};
});
I can move the http request over, but I'm not sure how to push the $viewValue over or have it accessible in the service as well.
app.factory('poService', function () {
});
Upvotes: 0
Views: 96
Reputation: 104795
Just add it as a parameter:
app.factory('poService', function ($http) {
return {
fetchPartNumbers: function(value) {
return $http.get('/api/GetInventoryById/' + value).then(function(res) {
var PartNumbers = [];
angular.forEach(res.data, function (item) {
PartNumbers.push(item.PartNumber);
});
return PartNumbers;
});
}
}
});
And the controller
$scope.fetchPartNumbers = function ($viewValue) {
poService.fetchPartNumbers($viewValue).then(function(data) {
console.log(data)
});
}
Upvotes: 2