我想知道在初始化工厂时是否可以使用角度工厂的构造函数来运行某些代码。例如,我有一个工厂服务,我想在应用程序启动时或者在调用/初始化此工厂时从中打印到控制台。有人可以解释一下吗?
Angular Factory:
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);
});
角度控制器:
app.controller('HomeController', function($scope, $state, HomeFactory) {
$scope.HomeFactory = HomeFactory;
}
);
答案 0 :(得分:1)
您创建的工厂只是一个构造函数。你需要实例化实例来调用它:
$scope.home1 = new HomeFactory();
$scope.home2 = new HomeFactory();