app.provider('$helloWorld', function() { return { world: 'World', $get: function($hello) { return $hello + this.world; } } });
$helloWorld = function($hello) { return $hello + this.world; }
$helloWorldProvider = { world: 'World' }
app.config(function($helloWorldProvider) { $helloWorldProvider.world = 'Piter'; })
app.controller('MainCtrl', function($scope, $helloWorld) { $scope.title = $helloWorld; // $hello + 'Piter' });
app.provider('$helloWorld', function() { return { $get: function($hello) { return $hello + 'World'; } } });
app.factory('$helloWorld', function($hello) { return $hello + 'World'; });
$helloWorldProvider
, only it will be without parameters - empty.
factory, service, value, constant
- just syntactic sugar for the provider
$hello
is just some kind of our service, taken as an example:
app.factory('$hello', function() { return 'Hello'; });
app.provider('$helloWorld', function() { return { world: 'World', $get: function($hello) { return $hello + this.world; } } });
app.factory('$helloWorld', function($hello) { return $hello + 'World'; }); // app.provider('$helloWorld', function() { return { $get: function($hello) { return $hello + 'World'; } } });
app.service('$helloWorld', HelloWorldClass); // app.provider('$helloWorld', function() { return { $get: function($hello) { return new HelloWorldClass($hello); } } });
app.value('$helloWorld', {greating: 'Hello'}); // app.provider('$helloWorld', function() { return { $get: function() { return {greating: 'Hello'}; } } });
app.constant('$helloWorld', {world: 'Piter'}); // app.provider('$helloWorld', function() { return { world: 'Piter', $get: function() { return this; // $get } } }); // config Provider app.config(function($helloWorld) { ... })
function HelloWorldClass($hello) { ... } app.service('$helloWorld', HelloWorldClass);
HelloWorldClass
(and any function in $get
) starts through $injector.invoke(fn)
, and this is the kind of thing that reads the function code, pulls out arguments from parentheses and pulls in services of the same name ( $hello
in our case) . It's simple!Source: https://habr.com/ru/post/221733/