我正在创建简单的入门应用程序来玩角度2,我正在尝试做一个待办事项服务并将他注入我的组件,我得到这个错误:
没有TodoService的提供商!(TodoList - > TodoService)
TodoService.ts
export class TodoService {
todos: Array<Object>
constructor() {
this.todos = [];
}
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
问题是什么?
注射剂是在@Component不在上面指定的@View
你有:
@Component({
selector: 'my-app'
})
@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService] // moving this line
})
Run Code Online (Sandbox Code Playgroud)
将其更改为:
@Component({
selector: 'my-app',
injectables: [TodoService] // to here
})
@View({
templateUrl: 'app.html',
directives: [For, If]
})
Run Code Online (Sandbox Code Playgroud)
这将允许DI将其拾取并将其注入您的组件.
| 归档时间: |
|
| 查看次数: |
26143 次 |
| 最近记录: |