i wonder if there is a proper way to inject interfaces in Angular2? (cf. below)
I think this is related with the missing @Injectable() decorator on the interface, bu
Alternate solution for angular 9
@Injectable()
export class TodoListPublicService implements TodoListService {
getTodos(): Todo[] {
const todos: Todo[] = [
{
title: 'get groceries',
description: 'eggs, milk, etc.',
done: false
}
];
return todos;
}
}
create an abstract class
export interface Todo {
title: string;
description: string;
done: boolean;
}
@Injectable()
export abstract class TodoListService {
abstract getTodos(): Todo[];
}
Use in the component
providers: [
{ provide: TodoListService, useClass: TodoListPublicService }
]
export class TodoListComponent implements OnInit {
todos: Todo[];
constructor(private todoListService: TodoListService) { }
ngOnInit() {
}