标签:on() code imp org .com provider exp nts inject
//配置已创建的服务:
import { MemberService } from "./member.service";
@NgModule({ // ... providers: [MemberService] }) export class AppModule { } //导入已创建的服务 import { MemberService } from ‘../member.service‘; //使用构造注入方式,注入服务 export class MembersComponent implements OnInit { // ... constructor(private memberService: MemberService) { } }
import { Injectable } from ‘@angular/core‘; import { Http } from ‘@angular/http‘; @Injectable() export class MemberService { constructor(private http: Http) { } getMembers() { return this.http .get(`https://api.github.com/orgs/angular/members?page=1&per_page=5`) .map(res => res.json()) } }
import { MemberService } from "./member.service"; @Component({...}) export class MembersComponent implements OnInit { members: Member[]; constructor(private memberService: MemberService) { } ngOnInit() { this.memberService.getMembers() .subscribe(data => { if (data) this.members = data; }); } }
标签:on() code imp org .com provider exp nts inject
原文地址:http://www.cnblogs.com/avidya/p/7464928.html