Reputation: 6394
I am struggling with a problem with Angular and Typescript.
module App {
declare var environment;
export var app: ng.IModule = angular.module("budgetsApp", [])..factory("Company", CompanyFactory)
.controller('mainMenuController', MainMenuController);
--Typescript file
module App {
declare var _; //the underscore library
export interface ICompanyResult {
companyId: number;
name: string;
}
export interface ICompanyFactoryFindResults {
count: number;
values: ICompanyResult[];
}
export class CompanyFactory {
static $inject = ['$http'];
private $http: any;
constructor($http: ng.IHttpService) {
this.$http = $http;
}
public find(searchTerm: any, limit: any): ICompanyFactoryFindResults {
return this.$http
.get('api/company')
.then((result) => {
var results = _.filter(result.data, (item) => {
return item.name.search(new RegExp(searchTerm, 'i')) !== -1;
});
return {
count: result.data.length,
values: _.take(results.limit)
};
});
}
}
};
Error I get in console is:
Error: [$injector:undef] Provider 'Company' must return a value from $get factory method.
Upvotes: 0
Views: 3310
Reputation: 2191
I was helped by Girafa's reply after modifying the code as follows;
class Company {...}
function CompanyFactory () {
return new Company();
}
...
app.factory('Company', CompanyFactory);
Upvotes: 1
Reputation: 3822
Factory in angular should be registered as function. The return value of that function will be the value of factory.
You should wrap your class with a function:
class Company {...}
export function CompanyFactory () {
return Company;
}
...
app.factory('Company', CompanyFactory);
This code is useful for cases where you need to create instances of Company
class manually. If Company
is singleton, register it as service
:
app.service('Company', Company);
Upvotes: 0