javascript AngularJS 工厂如何返回一个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20127199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can AngularJS factory return an object
提问by user1834664
I have a requirement in which i should write factory. This factory should contain 3 function init, save and delete
我有一个要求,我应该写工厂。这个工厂应该包含3个函数init,save和delete
I should call init function from controller. This function returns an object. This object has the function to execute the add and delete function.
我应该从控制器调用 init 函数。该函数返回一个对象。该对象具有执行添加和删除功能的功能。
how can i achieve this?
我怎样才能做到这一点?
Following is my code, It execute the init function successfully but when i try to use the object which was returned in add or delete, it says object is empty
以下是我的代码,它成功执行了 init 函数,但是当我尝试使用在添加或删除中返回的对象时,它说对象为空
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?
我怎样才能做到这一点?
回答by m59
First of all, you aren't returning a factory, but a service. It's a factory that creates a service, so adjust your naming convention like so: app.factory('someService'
首先,您返回的不是工厂,而是服务。它是一个创建服务的工厂,所以像这样调整你的命名约定:app.factory('someService'
Your code is a mess 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();
在您的控制器中: someService.add();
回答by Nick Steele
A factory and service are actually different in onesimple but very, very important way.
工厂和服务实际上在一个简单但非常非常重要的方面有所不同。
A Factoryreturns services that can be new'd.
一个工厂返回一个可以new'd服务。
In other words, you can do this:
换句话说,你可以这样做:
var instanceOne = new $factory;
var instanceTwo = new $factory;
...and then call them as separate entities. This is in fact what the factory pattern is all about.
...然后将它们称为单独的实体。这实际上就是工厂模式的全部内容。
Here is a simple example of a factory that you can get copies of...
这是一个工厂的简单示例,您可以获得...
app.factory('myFactory', function() {
return {
talk: function(what) {
return "Say " + what;
}
}
});
If instead you want a Service, you need to reference methods inside it by this.methodName, then you're just returning the service itself, or, a singleton pattern. Like so...
如果你想要一个Service,你需要通过 this.methodName 引用它里面的方法,那么你只是返回服务本身,或者,一个单例模式。像这样...
app.service('myService', function() {
this.talk = function(what) {
return "Say " + what;
};
});
As a side note; if you look at my code, Angular now supports actually defining services via the service keyword, or, the factory keyword. In the source, you can see that they both literally reference the same code.
作为旁注;如果您查看我的代码,Angular 现在支持通过 service 关键字或 factory 关键字实际定义服务。在源代码中,您可以看到它们都从字面上引用了相同的代码。
That's how similar they are, and in fact these two factory and service methods are interchangeable, so, if you got confused at first, don't sweat it; there is almost no difference at all... almost:)
这就是它们的相似之处,实际上这两种工厂和服务方法是可以互换的,因此,如果您一开始感到困惑,请不要担心;几乎没有区别......几乎:)
... lots of people seem to want to know: "How can you return a factory or service that just has one function, i.e., it, itself, is the function?" Easy.. like this...
……很多人似乎想知道:“你怎么能返回一个只有一个功能的工厂或服务,即它本身就是功能?” 容易……像这样……
app.factory('myFactory', function() {
return function(what) {
return "Say " + what;
}
});
回答by Nejmeddine Jammeli
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
});