Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  89] [ 4]  / answers: 1 / hits: 26713  / 9 Years ago, thu, may 14, 2015, 12:00:00

I am creating simple starter app to play with angular 2 and i am trying to make a todo service and inject him to my component and i get this error:



No provider for TodoService! (TodoList -> TodoService)



TodoService.ts



export class TodoService {
todos: Array<Object>
constructor() {
this.todos = [];
}
}


app.ts



/// <reference path=typings/angular2/angular2.d.ts />

import {Component, View, bootstrap, For, If} from 'angular2/angular2';
import {TodoService} from './TodoService'

@Component({
selector: 'my-app'
})

@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService]
})

class TodoList {
todos: Array<Object>
constructor(t: TodoService) {
this.todos = t.todos
}

addTodo(todo) {
this.todos.push({
done:false,
todo: todo.value
});
}
}

bootstrap(TodoList);


What is the problem?


More From » angular

 Answers
13

The injectables are specified on the @Component not on the @View



You have:



@Component({
selector: 'my-app'
})

@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService] // moving this line
})


Change it to:



@Component({
selector: 'my-app',
injectables: [TodoService] // to here
})

@View({
templateUrl: 'app.html',
directives: [For, If]
})


This will allow DI to pick it up and inject it into your component.


[#66605] Tuesday, May 12, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacyl

Total Points: 131
Total Questions: 105
Total Answers: 94

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
stacyl questions
Thu, Jan 28, 21, 00:00, 3 Years ago
Sun, Mar 8, 20, 00:00, 4 Years ago
Tue, Feb 25, 20, 00:00, 4 Years ago
Tue, Feb 11, 20, 00:00, 4 Years ago
;