如何在Angular中将参数传递给工厂的初始化函数

时间:2023-01-26 00:36:54

I wonder when I init a factory, how can I pass params to it, like:

我想知道我什么时候开工厂,怎么能把params传给它,比如:

.factory("facObj", function(/*dependency here, but how to put init params*/){
    var facObj = {};
    facObj.name = params.name; // this params is the init params I want to pass in.
})

How to pass that params to factory?

如何将这些参数传递给工厂?

3 个解决方案

#1


3  

You can just expose a method to set whatever internal variables you want:

您可以公开一个方法来设置您想要的任何内部变量:

.factory('facObj', function() {
  var thing;

  return {
    set: function(value) {
      thing = value;
    },
    get: function() {
      return thing;
    }
  }
});


// elsewhere
.controller('MyCtrl', function($scope, facObj) {
   facObj.set('awesome');

   $scope.thing = facObj.get(); // => 'awesome'
});

You can't initialize it with a set of parameters in the way you would normally think. The "contract" so-to-speak of a factory is that it returns an object. You can leverage that object to hold methods that manipulate whatever internal data you have.

您无法以通常认为的方式使用一组参数对其进行初始化。所谓工厂的“合同”就是它返回一个对象。您可以利用该对象来保存操纵您拥有的任何内部数据的方法。

#2


2  

Because factories are singletons, they are only initiated once. therefor, the function where you define them is their init. I'm not sure what you are trying to do, but if you want to give the variables in the factory an initial value, you set it hardcoded

因为工厂是单身人士,所以他们只启动一次。因此,您定义它们的函数是它们的init。我不确定你要做什么,但是如果你想在工厂给变量一个初始值,你就把它设置为硬编码

.factory("facObj", function(dependency){
    var facObj = {};
    facObj.name = "name";
    return {
      ...
    };
})

if you want to initiate them from a controller, you can define a set method

如果要从控制器启动它们,可以定义set方法

.factory("facObj", function(dependency){
    var facObj = {};
    var setName = function(newName) {
      facObj.name = newName;
    };
    return {
      ...
      setName:  setName
    };
})

#3


0  

A pattern I use for such a thing looks like this…

我用这种东西的模式看起来像这样......

Factory code…

工厂代码......

angular.module('myApp').factory('MyFactory', function () {

  var myVar;

  function MyFactory(param) {
    myVar = param;
    this.hello = hello;
  }

  function hello() {
    console.log('Hello ' + myVar);
  }

  return MyFactory;
});

Then when using the factory and needing to pass parameters…

然后在使用工厂并需要传递参数时......

angular.module(‘myApp’).controller(‘MyController’ function(MyFactory) {
   myFactory = new MyFactory(‘world’);
   myFactory.hello();
});

#1


3  

You can just expose a method to set whatever internal variables you want:

您可以公开一个方法来设置您想要的任何内部变量:

.factory('facObj', function() {
  var thing;

  return {
    set: function(value) {
      thing = value;
    },
    get: function() {
      return thing;
    }
  }
});


// elsewhere
.controller('MyCtrl', function($scope, facObj) {
   facObj.set('awesome');

   $scope.thing = facObj.get(); // => 'awesome'
});

You can't initialize it with a set of parameters in the way you would normally think. The "contract" so-to-speak of a factory is that it returns an object. You can leverage that object to hold methods that manipulate whatever internal data you have.

您无法以通常认为的方式使用一组参数对其进行初始化。所谓工厂的“合同”就是它返回一个对象。您可以利用该对象来保存操纵您拥有的任何内部数据的方法。

#2


2  

Because factories are singletons, they are only initiated once. therefor, the function where you define them is their init. I'm not sure what you are trying to do, but if you want to give the variables in the factory an initial value, you set it hardcoded

因为工厂是单身人士,所以他们只启动一次。因此,您定义它们的函数是它们的init。我不确定你要做什么,但是如果你想在工厂给变量一个初始值,你就把它设置为硬编码

.factory("facObj", function(dependency){
    var facObj = {};
    facObj.name = "name";
    return {
      ...
    };
})

if you want to initiate them from a controller, you can define a set method

如果要从控制器启动它们,可以定义set方法

.factory("facObj", function(dependency){
    var facObj = {};
    var setName = function(newName) {
      facObj.name = newName;
    };
    return {
      ...
      setName:  setName
    };
})

#3


0  

A pattern I use for such a thing looks like this…

我用这种东西的模式看起来像这样......

Factory code…

工厂代码......

angular.module('myApp').factory('MyFactory', function () {

  var myVar;

  function MyFactory(param) {
    myVar = param;
    this.hello = hello;
  }

  function hello() {
    console.log('Hello ' + myVar);
  }

  return MyFactory;
});

Then when using the factory and needing to pass parameters…

然后在使用工厂并需要传递参数时......

angular.module(‘myApp’).controller(‘MyController’ function(MyFactory) {
   myFactory = new MyFactory(‘world’);
   myFactory.hello();
});