Can someone please have a look at this code and help to figure out why i keep getting this error. I am new to angularjs (particularly the use of service/factory functions). It was working well when the function and local variable were defined in a controller (instead of a $service) as $scope variables, but because the data returned by this function is required by multiple other controllers , i wanted to reduce code redundancy.
app.service('SupportService', function()
{
var locations = []; //local variable to which other elements should be added
//function to get data and update the local variable (this.locations)
this.setLocations = function()
{
var temp_location = {};
$.ajax
({
type: 'get',
url: 'myurl',
success: function(response)
{
for(var i = 0; i < response.length; i++)
{
temp_location =
{
id: response[i].id,
name: response[i].name
};
locations.push(temp_location); //this is where the error is
}
},
error: function(response)
{
}
});
}
an additional function for accessing the locations variable from the service is
this.getLocations = function()
{
return locations;
}
The controller that i use to bind the data is as follows
app.controller('RSVPController', function($scope,SupportService,AuthService)
{
$scope.init = function()
{
SupportService.setLocations();
}
$scope.locations = SupportService.getLocations();
});
and in the view i have a call to the init function of this controller and are appending the values to the select as follows
<select ng-model="location" ng-options="item.name for item in locations track by item.id" required></select>
thank you.