我正在尝试创建工厂的新实例,但是它始终打印最后一个实例(因为它的单例)。new
每次使用关键字创建时如何实现新实例?我创建了我试图在 jsfiddle.net 中实现的示例谢谢。
angular.module('mainModule', []);
Crud.$inject = ['$http'];
function Crud($http) {
function CrudFactory(crudDTO) {
var vm = CrudFactory;
vm.x = 1;
vm.addX = addX;
vm.getX = getX;
vm.print = print;
function addX(){
vm.x +=1;
}
function getX(){
return vm.x;
}
function print(){
console.log(crudDTO.entity);
}
return vm;
}
return CrudFactory;
}
angular
.module('mainModule')
.factory('Crud', Crud);
angular.module('mainModule').controller('mainCtrl', function($scope,Crud){
var a = new Crud({entity:"test1"});
var b = new Crud({entity:"test2"});
//a.addX();
//$scope.a =b.getX();
a.print();
b.print();
a.print();
});