我在将类加载到Angular组件时遇到了问题.我一直试图解决它很长一段时间; 我甚至尝试在一个文件中加入它.我有的是:
Application.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
import {Component,View,bootstrap,NgFor} from "angular2/angular2";
import {NameService} from "./services/NameService";
@Component({
selector:'my-app',
injectables: [NameService]
})
@View({
template:'<h1>Hi {{name}}</h1>' +
'<p>Friends</p>' +
'<ul>' +
' <li *ng-for="#name of names">{{name}}</li>' +
'</ul>',
directives:[NgFor]
})
class MyAppComponent
{
name:string;
names:Array<string>;
constructor(nameService:NameService)
{
this.name = 'Michal';
this.names = nameService.getNames();
}
}
bootstrap(MyAppComponent);
Run Code Online (Sandbox Code Playgroud)
服务/ NameService.ts
export class NameService {
names: Array<string>;
constructor() {
this.names = ["Alice", "Aarav", "Martín", "Shannon", "Ariana", "Kai"];
}
getNames()
{
return this.names;
}
}
Run Code Online (Sandbox Code Playgroud)
我一直收到一条错误信息No …
我正在创建简单的入门应用程序来玩角度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)
问题是什么?