Skip to content

Commit 0f6deaa

Browse files
Merge pull request #10 from pBouillon/fix-chapter-7
Fix typos and improve code snippets in chapter 7
2 parents 73ea399 + 295feaa commit 0f6deaa

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

docs/chapter-7.mdx

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ ng generate component CategoryListPresenter
4343

4444
Now we have two components. Let's start with the presenter, because it is the simple one. Basically, we just need to receive the array of categories as an `@Input` property, and display the categories using `*ngFor`. Let's write that code:
4545

46-
```ts
47-
// src/app/category-list/category-list-presenter/category-list-presenter.component.ts
46+
```ts title="src/app/category-list/category-list-presenter/category-list-presenter.component.ts"
4847
import { Component, Input, ChangeDetection } from "@angular/core";
4948

5049
// import the Category interface from wherever you put it
@@ -61,9 +60,9 @@ export class CategoryListPresenter {
6160

6261
The component class code is ready - simple as that. Now, to display the list of categories, let's import some components from Angular Material into our `AppModule`. We will be using the `<mat-list>` component to display a very basic list of our categories:
6362

64-
```ts
65-
// src/app/app.module.ts
63+
```ts title="src/app/app.module.ts"
6664
// other import statements omitted for the sake of brevity
65+
6766
import { MatListModule } from "@angular/material";
6867

6968
@NgModule({
@@ -79,23 +78,19 @@ export class AppModule {}
7978

8079
We are all set to write the template for the presenter component:
8180

82-
```html
83-
<--
84-
src/app/category-list/category-list-presenter/category-list-presenter.component.html
85-
-->
81+
```html title="src/app/category-list/category-list-presenter/category-list-presenter.component.html"
8682
<mat-list>
87-
<mat-list-item *ngFor="let category of categories"
88-
>{{ category.name }}</mat-list-item
89-
>
90-
<.mat-list></mat-list
91-
>
83+
<mat-list-item *ngFor="let category of categories">
84+
{{ category.name }}
85+
</mat-list-item>
86+
</mat-list>
9287
```
9388

94-
Now the only thing left is to select the data from the `Store` in the container component, and pass it as `@Input` to the presenter. To select the data, we need to inject the `Store` into our component. The `Store` is a special service-class from NgRx that allows us to interact with the `State`. To inject that store, we need to import the `StoreModule` into our `AppModule`, and provide the `Reducer` from our previous chapter, so that NgRx knows what is our data an how it is modified. Let;s do this:
89+
Now the only thing left is to select the data from the `Store` in the container component, and pass it as `@Input` to the presenter. To select the data, we need to inject the `Store` into our component. The `Store` is a special service-class from NgRx that allows us to interact with the `State`. To inject that store, we need to import the `StoreModule` into our `AppModule`, and provide the `Reducer` from our previous chapter, so that NgRx knows what is our data an how it is modified. Let's do this:
9590

96-
```ts
97-
// src/app/app.module.ts
91+
```ts title="src/app/app.module.ts"
9892
// other import statements omitted for the sake of brevity
93+
9994
import { StoreModule } from "@ngrx/store";
10095
import { reducer } from "./state/reducer";
10196

@@ -116,8 +111,7 @@ Now it is time to write the `Selectors`.
116111

117112
Selectors are functions, as you remember from a past paragraph. We usually put them in separate files. In the `state` folder, create a new file named `selectors.ts`, and let's write our first selector, that return the list of the categories:
118113

119-
```ts
120-
// src/app/state/selectors.ts
114+
```ts title="src/app/state/selectors.ts"
121115
import { AppState } from "./state";
122116

123117
export const categories = (state: { categories: AppState }) =>
@@ -138,17 +132,18 @@ and returns that `categories` array.
138132

139133
Now let's use it in the container component. As mentioned, we inject the `Store` and select the state slice using our brand new selector. Here is how it will look like:
140134

141-
```ts
142-
// src/app/category-list/category-list-container/category-list-container.component.ts
135+
```ts title="src/app/category-list/category-list-container/category-list-container.component.ts"
143136
import { Component } from "@angular/core";
144137
import { Store } from "@ngrx/store";
145138

146139
import { categories } from "../../state/selectors";
147140

148141
@Component({
149142
selector: "app-category-list-presenter",
150-
template:
151-
'<app-category-list-presenter [categories]="categories$ | async"></app-category-list-presenter>',
143+
template: `
144+
<app-category-list-presenter [categories]="categories$ | async">
145+
</app-category-list-presenter>
146+
`,
152147
})
153148
export class CategoryListContainer {
154149
categories$ = this.store.select(categories);
@@ -163,9 +158,7 @@ And that's it. Let's understand what happened here. First, we imported and injec
163158
164159
Now let's create some routing. Let's create a routing module and a route that takes us to the category list page. Create an `app.routing.module.ts` file and put the following code in it:
165160

166-
```ts
167-
// src/app/app.routing.module.ts
168-
161+
```ts title="src/app/app.routing.module.ts"
169162
import { NgModule } from "@angular/core";
170163
import { RouterModule, Routes } from "@angular/router";
171164

@@ -186,9 +179,7 @@ export class AppRoutingModule {}
186179
187180
Now open your app and navigate to `/categories`. Aaaand... nothing. You won't see anything. That is because we don't have any categories in our `initialState`! It was an empty array, remember? Let's change it a bit, so that it contains the most basic expense everyone makes: Food:
188181

189-
```ts
190-
// src/app/state/state.ts
191-
182+
```ts title="src/app/state/state.ts"
192183
// rest of the file omitted for the sake of brevity
193184

194185
const initialState: AppState = {

0 commit comments

Comments
 (0)