Factories in Angular are considered an abstraction of providers, simplifying the process of returning data with less code. However, providers offer more flexibility as they allow for better definition of a service's functionality. I've been struggling to understand how to properly call a simple function from a provider. It seems like everyone does it differently and I can't seem to get mine to work:
myApp.provider('myProvider', function() {
myVar = true;
$get: function() {
return {
myVar: myVar,
}
},
toggleFalse = function() {
myVar = false;
},
})
myApp.controller('myController', function($scope, myProvider) {
myProvider.toggleFalse();
});
Unfortunately, this approach doesn't seem to be working - and services are still a bit confusing to me. The error message says toggleFalse
is not defined. How can I properly define a setter function on an Angular service that can be controlled through a controller?