How can AngularJS factory return an object

I have a requirement in which I have to write a factory. This factory should contain 3 init functions, save and delete

I have to call the init function from the controller. This function returns an object. This object has a function to perform the function of adding and removing.

how can i achieve this

Below is my code. It successfully executes the init function, but when I try to use an object that was returned in add or delete, it says that the object is empty.

angularApp.factory('SomeFactory', function(){ var client = new Client(); // this client is defined in another javascript file // this is the object which we should return var clientReady = function () { var cv = client.GetVersion(); showIDs(); }; return { initClient:function(requiredUID){ client.setAttribute("clientReadyCallback",clientReady); }//, }; var add = function () { client.someapi; }; var delete = function () { client.someapi; };` }); in controller i call the below calls SomeFactory.initClient("username"); SomeFactory.add();// throws error 

How can i achieve this?

+6
source share
3 answers

First of all, you are not returning the factory, but a service. This is the factory that creates the service, so adjust your naming convention like this: app.factory('someService'

Your code is messy and has errors, so I’ll just show you the basics of returning a service object.

 app.factory('someService', function() { var someService = { //build this object however you want add: function() { }, save: function() { } }; return someService; //return the object }); //end factory 

in your controller: someService.add();

+18
source

This is an Authentification factory from a real project.

 //factory using firebase Athentication service myApp.factory('Authentication', function($firebase,$firebaseAuth,$location){ var ref = new Firebase("URL_to_firebase"); var simpleLogin = $firebaseAuth(ref); var myObject = { //user is the data from the FORM login : function(user){ return simpleLogin.$authWithPassword({ email: user.email, password: user.password }); }//login }//password return myObject;//factory should return something }); //and then you call like this in your controller myApp.controller('myController' , function($scope, $location, $firebaseAuth,Authentication){ $scope.login = function() { Authentication.login($scope.user) } //login }); 
+2
source

A factory, and the service is really different in a simple one simple, but very important way.

A Factory returns services that may be new'd.

In other words, you can do this:

 var instanceOne = new $factory; var instanceTwo = new $factory; 

... and then call them separate objects. This is actually a factory pattern.

Here is a simple factory example where you can get copies ...

 app.factory('myFactory', function() { return { talk: function(what) { return "Say " + what; } } }); 

If instead you want a Service , you need to reference the methods inside that .methodName name, then you simply return the service itself or the singleton template. So ...

 app.service('myService', function() { this.talk = function(what) { return "Say " + what; }; }); 

As a side note; if you look at my code, Angular now supports the actual definition of services using the service keyword or the factory keyword. In the source you can see that they both literally refer to the same code.

How similar they are, and in fact these two factory and service methods are interchangeable, so if you get confused at first, don't sweat; almost no difference ... almost :)

... many people seem to want to know: "How can you return a factory or service that has only one function, i.e. is it a function itself?" Easy .. like that ...

 app.factory('myFactory', function() { return function(what) { return "Say " + what; } }); 
+1
source

Source: https://habr.com/ru/post/958583/


All Articles