AngularJS Services Independent of Multi-Page Modules

I have a user table in the database behind my angular application. I have separate accounts for logging in, registering and managing users as separate pages.

I want to define a service that stores a list of user objects as they are loaded. If a new query is requested from the database, the record must be transferred to the service, and the service must return the existing object, if any, or create a new one otherwise. In addition, the service must be able to disable unused objects when specified, and these objects must be installed on uninitialized user objects. If a create call is subsequently created, instead of creating a new object, it should just install the old object.

I also want to define a service (but should it not be called a service?), Which defines a user object. Thus, the user will not be just a parsed JSON object, but will also be decorated with methods for tracking any changes and either committing or reverting these changes.

Since I have separate pages that use a custom object. I would like to include services on every page. According to AngularJS docs, in order to register a service, I need to do something like:

var myModule = angular.module('myModule', []);
myModule.factory('serviceId', function() {
  var shinyNewServiceInstance;
  //factory function body that constructs shinyNewServiceInstance
  return shinyNewServiceInstance;
});

However, this service (as far as I can tell) is built-in. I do not want to copy and paste the code. I want to define a service in a separate file. The service does not need to know which module calls it. It must exist only to join the module.

I tried to create a separate file services/user.jswith the following syntax:

$provide.service('users', UserService);

, , , $ undefined. , $provide , .

, ? , , ?

+4
2

. , , .

myBackend, , , .

myBackend:

var myBackend = angular.module('myBackend', []);
myBackend.factory('backendSerivce', function() {
  var shinyNewServiceInstance;
  //factory function body that constructs shinyNewServiceInstance
  return shinyNewServiceInstance;
});

:

var myModule = angular.module('myModule', [ 'myBackend' ]);

myModule.controller('myShinyController', function (backendService) {
  // ...
});

JSON User .

+9

, , . , - , $provision . :

var myApp = angular.module('myApp',[]);
myApp.config(function($controllerProvider,$filterProvider,$compileProvider,$provide){
    myApp.register =
        {
            controller: $controllerProvider.register,
            directive: $compileProvider.directive,
            filter: $filterProvider.register,
            factory: $provide.factory,
            service: $provide.service,
            constant: $provide.constant
        };
});

myApp.register... factory.. service..etc. angular + require js

+5

All Articles