When I invoke a factory method within the controller, everything works smoothly. However, when I call a factory method within a specific controller method, it returns as undefined.
Below is my factory setup:
app.factory('config', ['$http', '$interval', function($http, $interval, $scope) {
return {
lines: function() {
return $http({
url: appPath+'/config.php?data=lines',
method: 'JSON'
})
},
updateLines: function() {
$http.post(appPath+'/write.php?handler=update line', $scope.lines).
success(function(data, status, headers, config) {
return true;
}).error(function(data, status, headers, config) {
return false;
});
}
}
}]);
My Controller Implementation:
app.controller('lineController', function($scope, $interval, $http, config) {
// This section executes successfully
config.lines().success(function(data) {
$scope.lines=data;
});
this.addToLine = function(line, customer) {
};
this.unassign = function(customer, line) {
var index = line.indexOf(customer);
line.splice(index, 1);
// Error encountered: Cannot read property 'lines' of undefined
config.updateLines();
};
});
In the context of the controller's scope, my factory config is well-defined. However, there seems to be an issue when attempting to reference config within a specific method, leading to the factory being undefined. As I am new to AngularJS, any guidance on best practices in such scenarios would be highly appreciated.
Thank you!