我正在创建简单的入门应用程序来玩 angular 2,我正在尝试制作待办事项服务并将其注入(inject)我的组件,但出现此错误:
没有 TodoService 的提供者! (TodoList -> TodoService)
TodoService.ts
export class TodoService {
todos: Array<Object>
constructor() {
this.todos = [];
}
}
应用.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);
问题是什么?
最佳答案
可注入(inject)物在 @Component
上指定,而不是在 @View
上指定
你有:
@Component({
selector: 'my-app'
})
@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService] // moving this line
})
将其更改为:
@Component({
selector: 'my-app',
injectables: [TodoService] // to here
})
@View({
templateUrl: 'app.html',
directives: [For, If]
})
这将允许 DI 获取它并将其注入(inject)到您的组件中。
关于javascript - Angular 2没有提供者错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30238057/