我在我的演示项目中添加了一项服务。
import {Injectable} from "@angular/core";
@Injectable()
export class TodoService {
todos = [];
}
并将其注入我的组件中。
import {Component} from "@angular/core";
import {TodoService} from "./todo-service";
@Component({
selector :'todo-input',
template: '<div><button (click)="btnClick(in)">Add item</button><input type="text" #in/></div>'
})
export class Todo {
constructor(public todoservice: TodoService) {
}
btnClick(val) {
this.todoservice.todos.push(val.value);
console.log(this.todoservice.todos);
}
}
我收到此错误
or: Error: Can't resolve all parameters for Todo: (?).
at CompileMetadataResolver.getDependenciesMetadata (https://unpkg.com/@angular/compiler/bundles/compiler.umd.js:14388:21)
at CompileMetadataResolver.get
答案 0 :(得分:2)
将您的app/todo-service.js
文件更改为app/todo-service.
t s
然后将其添加到组件或模块的providers
列表中:
import {TodoService} from "./todo-service";
@Component({
selector :'todo-input',
providers: [TodoService], <== this line
template: '..'
})
export class Todo {
<强> Plunker 强>