Skip to content

Instantly share code, notes, and snippets.

@yngvebn
Last active September 17, 2015 06:42
Show Gist options
  • Save yngvebn/442b4da1f559f222dae6 to your computer and use it in GitHub Desktop.
Save yngvebn/442b4da1f559f222dae6 to your computer and use it in GitHub Desktop.
angular.controller('Test', function(MyProvider, MyService, MyFactory){
// returnerer akkurat det samme:
MyProvider.sayHello();
MyService.sayHello();
MyFactory.sayHello();
MyProvider.saySomethingElse(); // 'I might be a string from some configuration or whatevs'
});
angular.config(function(MyProviderProvider){
MyProviderProvider.configureWhatElseToSay ('I might be a string from some configuration or whatevs')
});
angular.provider('MyProvider', function(){
var data = {
whatElseToSay = '';
}
// can be called from angular.config
this.configureWhatElseToSay = function(whatToSay){
data.whatElseToSay = whatToSay;
}
function saySomethingElse(){
return data.whatElseToSay;
}
function sayHello(){
return 'Hello!';
}
function sayGoodbye(){
return 'Buhbye!';
}
// only stuff exposed by calling $get will be available in application runtime
this.$get = function(){
return {
sayHello: sayHello,
sayGoodbye: sayGoodbye,
saySomethingElse: saySomethingElse
}
}
});
angular.service('MyService', function(){
// only stuff on 'this.' will be exposed at runtime
this.sayHello = sayHello;
this.sayGoodbye = sayGoodbye;
function sayHello(){
return 'Hello!';
}
function sayGoodbye(){
return 'Buhbye!';
}
});
angular.factory('MyFactory', function(){
function sayHello(){
return 'Hello!';
}
function sayGoodbye(){
return 'Buhbye!';
}
// only stuff returned will be exposed at runtime
return {
sayHello: sayHello,
sayGoodbye: sayGoodbye
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment