You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/chapter-7.mdx
+19-28Lines changed: 19 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,8 +43,7 @@ ng generate component CategoryListPresenter
43
43
44
44
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:
// import the Category interface from wherever you put it
@@ -61,9 +60,9 @@ export class CategoryListPresenter {
61
60
62
61
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:
63
62
64
-
```ts
65
-
// src/app/app.module.ts
63
+
```ts title="src/app/app.module.ts"
66
64
// other import statements omitted for the sake of brevity
65
+
67
66
import { MatListModule } from"@angular/material";
68
67
69
68
@NgModule({
@@ -79,23 +78,19 @@ export class AppModule {}
79
78
80
79
We are all set to write the template for the presenter component:
<mat-list-item*ngFor="let category of categories">
84
+
{{ category.name }}
85
+
</mat-list-item>
86
+
</mat-list>
92
87
```
93
88
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:
95
90
96
-
```ts
97
-
// src/app/app.module.ts
91
+
```ts title="src/app/app.module.ts"
98
92
// other import statements omitted for the sake of brevity
93
+
99
94
import { StoreModule } from"@ngrx/store";
100
95
import { reducer } from"./state/reducer";
101
96
@@ -116,8 +111,7 @@ Now it is time to write the `Selectors`.
116
111
117
112
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:
@@ -138,17 +132,18 @@ and returns that `categories` array.
138
132
139
133
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:
@@ -163,9 +158,7 @@ And that's it. Let's understand what happened here. First, we imported and injec
163
158
164
159
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:
@@ -186,9 +179,7 @@ export class AppRoutingModule {}
186
179
187
180
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:
188
181
189
-
```ts
190
-
// src/app/state/state.ts
191
-
182
+
```ts title="src/app/state/state.ts"
192
183
// rest of the file omitted for the sake of brevity
0 commit comments