在CustomerService上找不到指令注释

时间:2016-09-09 13:56:02

标签: angular

错误非常直接,但我有CustomerService的注释,并通过Directives声明。

import {Injectable} from "@angular/core";
import {CustomerModel} from "../components/Customer/CustomerModel"

@Injectable()
export class CustomerService{

    customerList:CustomerModel[] = [
        new CustomerModel("Mr. "),
        new CustomerModel("Miss. "),
        new CustomerModel("Ms. "),
    ];

}

Customer.ts

export class CustomerModel{

    firstName:String = "";
    lastName:String = "";


    constructor(title:String = ""){

    }

}

CustomersComponent.ts

import {Component} from "@angular/core";
import {CustomerService} from "../../services/CustomerService"
import {CustomerModel} from "./CustomerModel";

@Component({
    selector:'customers',
    template: require('./Customer.html'),

    directives:[
        CustomerService
    ]
})

export class CustomersComponent {

    title:string = "Hello Drew";
    customer:CustomerModel = new CustomerModel();

    constructor(public service:CustomerService){

    }

    onSubmit(){
        this.service.customerList.push(this.customer);
        this.customer = new CustomerModel();
    }
}

我在https://github.com/drewjocham/Angular2TestApp的github上添加了这个,如果有人想看更多的话。

1 个答案:

答案 0 :(得分:0)

它是提供者,您必须将value添加到providers数组而不是指令:

CustomerService

但是在你继续进行之前,我会建议你更新到最新的Angular2候选版本(RC.6),但那只是一个建议:)。因此,如果您在@Component({ selector:'customers', template: require('./Customer.html'), providers:[ CustomerService ] }) 的构造函数中没有依赖注入,则CustomerService不是必需的