app.constant('fooConfig', { config1: true, config2: "Default config2" });
app.value('fooConfig', { config1: true, config2: "Default config2 but it can changes" });
app.factory('foo', function() { var thisIsPrivate = "Private"; function getPrivate() { return thisIsPrivate; } return { variable: "This is public", getPrivate: getPrivate }; }); // ... app.factory('bar', function(a) { return a * 2; });
foo.variable
in one place, it will change in other places too. app.service('foo', function() { var thisIsPrivate = "Private"; this.variable = "This is public"; this.getPrivate = function() { return thisIsPrivate; }; });
new Foo();
to create an instance of an object. Keep in mind that the same object will return in other places if you use this service there. app.factory('foo2', function() { return new Foobar(); }); function Foobar() { var thisIsPrivate = "Private"; this.variable = "This is public"; this.getPrivate = function() { return thisIsPrivate; }; }
Foobar
is a class , and we create an instance of it in our factory, use it for the first time, and then return it. Like the service, an instance of the Foobar
class will only be created once and the next time the factory returns the same instance again. app.service('foo3', Foobar);
app.provider('foo', function() { return { $get: function() { var thisIsPrivate = "Private"; function getPrivate() { return thisIsPrivate; } return { variable: "This is public", getPrivate: getPrivate }; } }; });
$get
function, which will be what we embed in other parts of our application. Therefore, when we inject foo
into the controller, the $get
function is injected $get
app.provider('foo', function() { var thisIsPrivate = "Private"; return { setPrivate: function(newVal) { thisIsPrivate = newVal; }, $get: function() { function getPrivate() { return thisIsPrivate; } return { variable: "This is public", getPrivate: getPrivate }; } }; }); app.config(function(fooProvider) { fooProvider.setPrivate('New value from config'); });
thisIsPrivate
beyond the $get
function, and then created the setPrivate
function to be able to change thisIsPrivate
in the configuration function. Why do you need to do this? Isn't it easier to just add a setter in a factory? There is another goal.restangular
. The provider allows us to configure it in advance for our purposes.nameProvider
as the name, not name
. name
specified in all other cases.$routeProvider
and $locationProvider
configure routing and html5mode, respectively.foo
service lacked the greet
function and you want to add it. Is it worth changing a factory? Not! You can decorate it: app.config(function($provide) { $provide.decorator('foo', function($delegate) { $delegate.greet = function() { return "Hello, I am a new function of 'foo'"; }; return $delegate; }); });
$provide
is what Angulyar uses to create all internal services. We can use it manually if we want, or simply use the functions provided in our modules (you need to use $provide
for decoration). $provide
has a decorator
function that allows us to decorate our services. It receives the name of the service being decorated, and the callback receives $delegate
, which is the original instance of the service.greet
function to the original service. Then returned a new modified service.greet
function. // function Person( json ) { angular.extend(this, json); } Person.prototype = { update: function() { // ( :P) this.name = "Dave"; this.country = "Canada"; } }; Person.getById = function( id ) { // -, Person id return new Person({ name: "Jesus", country: "Spain" }); }; // app.factory('personService', function() { return { getById: Person.getById }; });
Person
object that receives some JSON data to initialize the object. Then we created a function in our prototype (functions in the prototype for instances of Person
) and functions directly in Person
(similar to the class functions).Person
object based on the identifier that we pass (as it will be in real code) and each instance will be able to update itself. Now you just need to create a service that will use it.personService.getById
we create a new Person
object, so we can use this service in different controllers, and even when the factory is a singleton, it creates new objects. app.controller 'MainCtrl', ($scope, personService) -> $scope.aPerson = personService.getById(1) app.controller 'SecondCtrl', ($scope, personService) -> $scope.aPerson = personService.getById(2) $scope.updateIt = () -> $scope.aPerson.update() class Person constructor: (json) -> angular.extend @, json update: () -> @name = "Dave" @country = "Canada" @getById: (id) -> new Person name: "Jesus" country: "Spain" app.factory 'personService', () -> { getById: Person.getById }
Source: https://habr.com/ru/post/190342/
All Articles