
committed by
Alex Eagle

parent
e64b1e99c2
commit
53f91189a1
@ -0,0 +1,7 @@
|
||||
<h1>
|
||||
{{title}}
|
||||
</h1>
|
||||
|
||||
<li *ngFor="let user of users">
|
||||
<span >{{user.id}}</span> {{user.name}}
|
||||
</li>
|
32
aio/content/examples/providers/src/app/app.component.spec.ts
Normal file
32
aio/content/examples/providers/src/app/app.component.spec.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { TestBed, async } from '@angular/core/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [
|
||||
AppComponent
|
||||
],
|
||||
});
|
||||
TestBed.compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
}));
|
||||
|
||||
it(`should have as title 'app works!'`, async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.debugElement.componentInstance;
|
||||
expect(app.title).toEqual('app works!');
|
||||
}));
|
||||
|
||||
it('should render title in a h1 tag', async(() => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.debugElement.nativeElement;
|
||||
expect(compiled.querySelector('h1').textContent).toContain('app works!');
|
||||
}));
|
||||
});
|
26
aio/content/examples/providers/src/app/app.component.ts
Normal file
26
aio/content/examples/providers/src/app/app.component.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
import { User } from './core/user';
|
||||
import { UserService } from './core/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.css'],
|
||||
providers: [UserService]
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
title = 'Users list';
|
||||
users: User[];
|
||||
|
||||
constructor(private userService: UserService) { }
|
||||
|
||||
getUsers(): void {
|
||||
this.userService.getUsers().then(users => this.users = users);
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.getUsers();
|
||||
}
|
||||
|
||||
}
|
25
aio/content/examples/providers/src/app/app.module.ts
Normal file
25
aio/content/examples/providers/src/app/app.module.ts
Normal file
@ -0,0 +1,25 @@
|
||||
// #docplaster
|
||||
// #docregion app-module
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { HttpModule } from '@angular/http';
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
// CoreModule provides the UserService.
|
||||
import { CoreModule } from './core/core.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
FormsModule,
|
||||
HttpModule,
|
||||
CoreModule
|
||||
],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
||||
// #enddocregion app-module
|
16
aio/content/examples/providers/src/app/core/core.module.ts
Normal file
16
aio/content/examples/providers/src/app/core/core.module.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
import { UserService } from './user.service';
|
||||
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule
|
||||
],
|
||||
declarations: [],
|
||||
providers: [UserService]
|
||||
})
|
||||
export class CoreModule { }
|
14
aio/content/examples/providers/src/app/core/mock-users.ts
Normal file
14
aio/content/examples/providers/src/app/core/mock-users.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { User } from './user';
|
||||
|
||||
export const USERS: User[] = [
|
||||
{ id: 1, name: 'Maria' },
|
||||
{ id: 2, name: 'Alex' },
|
||||
{ id: 3, name: 'Chuntao' },
|
||||
{ id: 4, name: 'Béatrice' },
|
||||
{ id: 5, name: 'Sarah' },
|
||||
{ id: 6, name: 'Andrés' },
|
||||
{ id: 7, name: 'Abdul' },
|
||||
{ id: 8, name: 'Pierre' },
|
||||
{ id: 9, name: 'Jiao' },
|
||||
{ id: 10, name: 'Seth' }
|
||||
];
|
@ -0,0 +1,14 @@
|
||||
import { TestBed, inject } from '@angular/core/testing';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
describe('UserService', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [UserService]
|
||||
});
|
||||
});
|
||||
|
||||
it('should ...', inject([UserService], (service: UserService) => {
|
||||
expect(service).toBeTruthy();
|
||||
}));
|
||||
});
|
15
aio/content/examples/providers/src/app/core/user.service.ts
Normal file
15
aio/content/examples/providers/src/app/core/user.service.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
import { User } from './user';
|
||||
import { USERS } from './mock-users';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
|
||||
constructor() { }
|
||||
|
||||
|
||||
getUsers(): Promise<User[]> {
|
||||
return Promise.resolve(USERS);
|
||||
}
|
||||
}
|
4
aio/content/examples/providers/src/app/core/user.ts
Normal file
4
aio/content/examples/providers/src/app/core/user.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export class User {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
14
aio/content/examples/providers/src/index.html
Normal file
14
aio/content/examples/providers/src/index.html
Normal file
@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Providers</title>
|
||||
<base href="/">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
</head>
|
||||
<body>
|
||||
<app-root>Loading...</app-root>
|
||||
</body>
|
||||
</html>
|
11
aio/content/examples/providers/src/main.ts
Normal file
11
aio/content/examples/providers/src/main.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { enableProdMode } from '@angular/core';
|
||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||
|
||||
import { AppModule } from './app/app.module';
|
||||
import { environment } from './environments/environment';
|
||||
|
||||
if (environment.production) {
|
||||
enableProdMode();
|
||||
}
|
||||
|
||||
platformBrowserDynamic().bootstrapModule(AppModule);
|
Reference in New Issue
Block a user