angular 工厂中的变量是如何初始化的?

How are variables initialized in an angular factory?

我想知道 angular 工厂的构造函数是否可以在工厂初始化时用于 运行 一些代码。例如,我有一个工厂服务,我想在应用程序启动或工厂为 called/initialized 时从该服务打印到控制台。有人可以解释一下吗?

Angular工厂:

            app.factory('HomeFactory', function() {
            // create a object using constructor function
              var HomeFactory = function() {
                 console.log('in the constructor function');
              };
              HomeFactory.contactSubmit = function() {
                var self = this;
                console.log(self.model);
              }
              // returning the factory object
              return (HomeFactory);
              });

Angular 控制器:

  app.controller('HomeController', function($scope, $state, HomeFactory) {
    $scope.HomeFactory = HomeFactory;
}

);

您创建的工厂只是一个构造函数。你需要实例化实例来调用它:

$scope.home1 = new HomeFactory();
$scope.home2 = new HomeFactory();