Reputation: 1220
I have a controller that loads a view with a tabbed panel. Depending on what item the user clicked on, data is passed as a parameter when the function select
is invoked.
The first tab is working correctly, displaying data from the returned object.
The second tab consists of a graph. To make sure the correct data set is supplied to the graph, I need to fetch the id
from the returned object in the first panel. In the service loadPatentItemService
I created, when I log the returned item, it displays the correct information, though when I call this service from my controller lineCtrl
, it logs undefined
.
log within factory loadPatentItemService
service getPatent
console.log(item)
log returned value from calling service in lineCtrl
undefined
Could be that when I am calling it, it is calling it for a second time and the data is reset as no argument is being passed to the function.
Basically, why can't I access the data from my controller lineCtrl
from service loadPatentItemService
?
app.factory('loadPatentItemService', function() {
return {
select: function(item) {
var patentItem = [];
patentItem.push(item);
this.getPatent(patentItem);
return patentItem;
},
getPatent: function(item) {
// var hello = 'hello';
// console.log(item);
return item;
}
}
})
app.controller("lineCtrl", ['$scope', 'loadPatentItemService', function ($scope, loadPatentItemService) {
console.log(loadPatentItemService.getPatent());
}])
app.controller('patentListCtrl', ['$scope', 'loadPatentItemService', function($scope, loadPatentItemService) {
//FUNCTION INVOKED BY USER CLICKING ON ITEM IN TABLE
$scope.select = function(item) {
$scope.patentItem = loadPatentItemService.select(item);
}
}])
Upvotes: 1
Views: 40
Reputation: 2851
In the select
function, you need to store the selected item in the service so that you can send it back in the getPatent
function.
Try changing your your loadPatentItemService
to this
app.factory('loadPatentItemService', function() {
var service = {
selectedItem: null;
select: function(item) {
service.selectedItem = item;
return [selectedItem]; // Returning an array since your original post was returning an array
},
getPatent: function() {
return service.selectedItem ;
}
}
return service;
})
Upvotes: 2