未捕获的错误:意外的值' EmployeeService'由模块' AppModule'导入。请添加@NgModule注释

时间:2018-01-22 09:46:14

标签: angular angular-services

我无法将服务调用到应用组件中 app.component.ts



import { Component } from '@angular/core';
import { EmployeeService } from './services/employee.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  providers: [EmployeeService]
})
export class AppComponent {
  public title: string = 'Services Demo';
  public employeeObj: any
  public empId: string;

  constructor( private employeeService: EmployeeService){

  }

  public getEmployeeDetail(): void{
  	this.employeeObj = this.employeeService.getEmployee( this.empId );
  }

}




employee.service.ts:

import { Injectable } from '@angular/core';

@Injectable()

export class EmployeeService{

    public employeeRecord = [
        {name: 'sowmya', city: 'Hyd', age:'23', id:'e1'},
        {name: 'so ni', city: 'Banglore', age:'22', id:'e2'},
        {name: 'sujith', city: 'Chennai', age:'43', id:'e3'},
        {name: 'suruchi', city: 'Delhi', age:'41', id:'e4'}
    ];

    public getEmployee ( id:string ): any{
        let employee: any;

        for ( let i=0; i < this.employeeRecord.length; i++){
            if( id === this.employeeRecord[i].id ){
                employee = this.employeeRecord[1];
                break;
            }
        }
        return employee;
    }
}

0 个答案:

没有答案