I have a TypeScript class like this:
- it is not an Angular module
- assume that
personNameis a custom filter which cannot be used on view/template in this example
Code:
export class Person {
private $filter;
private name: string;
private sureName: string;
constructor($filter: ng.IFilterService, name: string, sureName: string) {
this.$filter = $filter;
this.name = name;
this.sureName = sureName;
}
public getName(): string {
return this.$filter('personName')(this.name, this.sureName);
}
}
This can be used in controller like this:
export class PeopleController {
private $filter: ng.IFilterService;
private person1 = new Person(this.$filter, 'Joe', 'Smith');
private person2 = new Person(this.$filter, 'John', 'Doe');
// ...
// private person100 = new Person(this.$filter, ...)
constructor($filter: ng.IFilterService) {
this.$filter = $filter;
}
}
angular.module('myApp').controller('PeopleController', PeopleController);
Is it possible to rewrite Person class to can be inited without the $filter? In other words, can I write Person class as Angular factory with dependency injection, then inject this factory into controller and make instances?
I'd like to have something like this:
export class PeopleController {
private PersonFactory: Person;
private person1 = new PersonFactory('Joe', 'Smith');
private person2 = new PersonFactory('John', 'Doe');
// ...
// private person100 = new Person(...)
constructor(PersonFactory: Person) {
this.PersonFactory = PersonFactory;
}
}
angular.module('myApp').factory('PeopleFactory', People);
angular.module('myApp').controller('PeopleController', PeopleController);