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/src/first-steps/README.md
+89-45Lines changed: 89 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,75 +50,116 @@ The `src` folder contains:
50
50
The `app` folder contains:
51
51
-`app.config.ts`: Defines the application configuration that tells Angular how to assemble the application.
52
52
-`app.routes.ts`: Defines the application's routing configuration
53
-
-`app.component.html`: Defines the HTML template associated with the root `AppComponent`
54
-
-`app.component.scss`: Defines the base stylesheet for the root `AppComponent`
55
-
-`app.component.ts`: Defines the logic for the app's root component, named `AppComponent`. The view associated with this root component is the root of the view hierarchy.
56
-
-`app.component.spec.ts`: Defines a unit test for the root `AppComponent`
53
+
-`app.html`: Defines the HTML template associated with the root `App` component.
54
+
-`app.scss`: Defines the base stylesheet for the root `App` component.
55
+
-`app.ts`: Defines the logic for the app's root component, named `App`. The view associated with this root component is the root of the view hierarchy.
56
+
-`app.spec.ts`: Defines a unit test for the root `App` component.
57
57
58
58
59
59
## Text interpolation in templates
60
60
61
-
Like any other component, the shell AppComponent is distributed over three files.
62
-
Open the component class file (`app.component.ts`) and change the value of the title property to 'Search films'
61
+
Like any other component, the shell `App` component is distributed over three files.
62
+
Open the component typescript file (`app.ts`) and change the value of the title property to 'Search films'
63
63
64
64
```typescript
65
65
// app.component.ts
66
66
import { Component } from'@angular/core'
67
67
68
68
@Component({
69
69
selector: 'app-root',
70
-
templateUrl: './app.component.html',
71
-
styleUrls: ['./app.component.scss']
70
+
templateUrl: './app.html',
71
+
styleUrls: ['./app.scss']
72
72
})
73
-
exportclassAppComponent {
74
-
title ='Search Films'
73
+
exportclassApp {
74
+
protectedreadonlytitle =signal('Search Films')
75
75
}
76
76
```
77
77
78
-
Open the component template file (`app.component.html`) and delete the default template generated by the Angular CLI. Replace it with the following line of HTML.
78
+
Open the component template file (`app.html`) and delete the default template generated by the Angular CLI. Replace it with the following line of HTML.
79
79
```html
80
-
<!-- app.component.html -->
81
-
<h1>{{title}}</h1>
80
+
<!-- app.html -->
81
+
<h1>{{title()}}</h1>
82
82
```
83
83
The double curly braces are Angular's *interpolation binding syntax*. This interpolation binding presents the component's title property value inside the HTML header tag.
84
84
85
85
The browser refreshes and displays the new application title.
86
86
87
-
The simplest way to insert data dynamically into your components is through text interpolation, using the `{{myVariable}}` syntax. Inside double curly braces, you can specify any valid JavaScript expression that don't have or promote side-effects.
87
+
The simplest way to insert data dynamically into your components is through text interpolation, using the `{{myVariable}}` syntax. Inside double curly braces, you can specify any valid JavaScript expression except for:
88
88
89
-
JavaScript expressions that have or promote side effects include:
90
-
- assignements (`=`, `+=`, ...)
91
-
- operators such as `new`, `typeof` or `instanceof`
92
-
- increment and decrement operators `++` and `--`
89
+
- bitwise operators: `&`, `&=`, `~`, `|=`, `^=`, etc.
The template has access to all the non-private members of the component class. Changing the visibility of `price` to `private` will render this error: *Property 'price' is private and only accessible within class 'AppComponent'.*
113
+
The template has access to all the non-private members of the component class. Changing the visibility of `price` to `private` will render this error: *Property 'price' is private and only accessible within class 'App'.*
110
114
111
-
Interpolation only works on textual content of elements. You can not use it to change the value of HTML attributes or to insert HTML code. For this, you will need to resort to directives, which we will see later in the training.
115
+
Interpolation only works on textual content of elements. You should not use it to change the value of HTML attributes and can't use it to insert HTML code. For this, we will see strategies later in the training.
112
116
113
117
In this example, we formatted the price manually. We will later see that Angular provides a way to declare reusable formatters: **pipes**.
114
118
115
119
116
120
## Working with components
117
121
118
-
The `AppComponent` is only the root component of an Angular application. A web application is made of small reusable components, embedded in higher level components to form the layout, the arrangement of your elements on the page. This structure can be described as a component tree. Angular creates, updates, and destroys components as the user moves through the application. The app can take action at each moment in this lifecycle through optional lifecycle hooks, like `ngOnInit()`.
122
+
The `App` component is only the root component of an Angular application. A web application is made of small reusable components, embedded in higher level components to form the layout, the arrangement of your elements on the page. This structure can be described as a component tree. Angular creates, updates, and destroys components as the user moves through the application. The app can take action at each moment in this lifecycle through optional lifecycle hooks, like `ngOnInit()`.
119
123
120
124

121
125
126
+
Before generating a second component, let's configure the naming convention of components and other key building blocks of an Angular app. Since Angular 20, the type of building block (component, service, directive) generated is not automatically appended to the file name and class name. That gives more flexibility to teams in their naming choices, however it can easily lead to naming clashs where the component, the service and the DTO have exactly the same class name for instance. This is why we will add the following configuration to the `angular.json` file to restore the appending of the building block name to the class name and file name.
127
+
128
+
```json{7-32}
129
+
// angular.json
130
+
"cli": {
131
+
"schematicCollections": [
132
+
"angular-eslint"
133
+
]
134
+
},
135
+
"schematics": {
136
+
"@schematics/angular:component": {
137
+
"type": "component"
138
+
},
139
+
"@schematics/angular:directive": {
140
+
"type": "directive"
141
+
},
142
+
"@schematics/angular:service": {
143
+
"type": "service"
144
+
},
145
+
"@schematics/angular:guard": {
146
+
"typeSeparator": "."
147
+
},
148
+
"@schematics/angular:interceptor": {
149
+
"typeSeparator": "."
150
+
},
151
+
"@schematics/angular:module": {
152
+
"typeSeparator": "."
153
+
},
154
+
"@schematics/angular:pipe": {
155
+
"typeSeparator": "."
156
+
},
157
+
"@schematics/angular:resolver": {
158
+
"typeSeparator": "."
159
+
}
160
+
}
161
+
```
162
+
122
163
Let's create a second component. It is advised to generate components using the [Angular CLI](https://angular.dev/cli/generate#component).
123
164
124
165
```bash
@@ -128,7 +169,7 @@ ng g c child #shorthand for ng generate component child
128
169
The `ng g c` command added a `child` folder containing the `ChildComponent` files in the `app` folder.
129
170
130
171
```typescript
131
-
// child.component.ts
172
+
// child.ts
132
173
import { Component } from'@angular/core'
133
174
134
175
@Component({
@@ -142,30 +183,30 @@ export class ChildComponent {
142
183
}
143
184
```
144
185
145
-
To link the components together, the child components are declared in their parent's component template, using their selector as a tag. The child component also needs to be added to the `imports` array of the parent component decorator. A component can be reused as many times as desired. The `ChildComponent`'s selector is `app-child`. Including this component as a child to the `AppComponent` is done as follows:
186
+
To link the components together, the child components are declared in their parent's component template, using their selector as a tag. The child component also needs to be added to the `imports` array of the parent component decorator. A component can be reused as many times as desired. The `ChildComponent`'s selector is `app-child`. Including this component as a child to the `App` component is done as follows:
146
187
147
188
<CodeGroup>
148
-
<CodeGroupItemtitle="app.component.html">
189
+
<CodeGroupItemtitle="app.html">
149
190
150
191
```html
151
-
<h1>{{title}}</h1>
192
+
<h1>{{title()}}</h1>
152
193
<app-child></app-child>
153
194
```
154
195
</CodeGroupItem>
155
-
<CodeGroupItemtitle="app.component.ts">
196
+
<CodeGroupItemtitle="app.ts">
156
197
157
198
```ts{2,6}
158
-
import { Component } from '@angular/core'
199
+
import { Component, signal } from '@angular/core'
159
200
import { ChildComponent } from './child/child.component'
160
201
161
202
@Component({
162
203
selector: 'app-root',
163
204
imports: [ChildComponent],
164
-
templateUrl: './app.component.html',
165
-
styleUrl: './app.component.scss'
205
+
templateUrl: './app.html',
206
+
styleUrl: './app.scss'
166
207
})
167
208
export class AppComponent {
168
-
title = 'Search Films'
209
+
protected readonly title = signal('Search Films')
169
210
}
170
211
```
171
212
</CodeGroupItem>
@@ -221,15 +262,18 @@ Here is the folder structure we will strive to achieve in the Search Films appli
This folder structure is best suited to simple projects with only one main feature that has its routes defined in one place, the `app-route.ts` file. As a project grows, feature folders and routes will be introduced and the structure can evolve to this:
265
+
This folder structure is best suited to simple projects with only one main feature that has its routes defined in one place, the `app.route.ts` file. As a project grows, feature folders and routes will be introduced and the structure can evolve to this:
The Angular team provides a [style guide](https://angular.dev/style-guide) where they advise to follow such a structure instead of the first one. When in doubt, refer to it.
271
+
228
272
:::tip
229
273
By default, the CLI will always generate in the `app` folder. You can tell it to generate in another folder by passing the path before the name of the element you want it to generate. For instance `ng generate component components/dashboard` will generate the `DashboardComponent` 4 files in `app/components/dashboard`. The `components` folder is created by the CLI if it doesn't already exist, as well as the `dashboard` folder.
230
274
:::
231
275
232
-
As the complexity of the folder structure of the application increases, it is a good practice to add aliases in the `tsconfig.json` file. Let's do it now to avoid a tedious refactoring later:
276
+
<!--As the complexity of the folder structure of the application increases, it is a good practice to add aliases in the `tsconfig.json` file. Let's do it now to avoid a tedious refactoring later:
233
277
```json
234
278
"compilerOptions": {
235
279
//...
@@ -243,16 +287,16 @@ As the complexity of the folder structure of the application increases, it is a
243
287
}
244
288
}
245
289
```
246
-
VsCode will automatically use those paths for the imports instead of relative ones that can be tough to read or maintain.
290
+
VsCode will automatically use those paths for the imports instead of relative ones that can be tough to read or maintain.-->
247
291
248
292
249
293
## Practical work: Your first component
250
294
251
-
1. If you created the `child` component, delete it as we won't need it moving forward and remove any reference to it in the `AppComponent`. You may get an error in the console where you launched the project once you delete the child component, in that case restart the project.
295
+
1. If you created the `app-child` component, delete it as we won't need it moving forward and remove any reference to it in the `App` component. You may get an error in the console where you launched the project once you delete the child component, in that case restart the project.
252
296
253
297
2. Most apps strive for a consistent look across the application. The CLI generated an empty `styles.scss` file for this purpose. Copy paste the content of the SCSS stylesheet that will serve as basis for all the practical work, downloadable here: [styles.scss](https://worldline.github.io/angular-training/styles.scss) in it.
254
298
255
-
3. Create a new component login-form containing the following authentication form (don't forget to generate it in the *components* folder):
299
+
3. Create a new component `login-form` containing the following authentication form (don't forget to generate it in the *components* folder):
256
300
```html
257
301
<formid="login-form">
258
302
<h1>Authentication</h1>
@@ -269,9 +313,9 @@ VsCode will automatically use those paths for the imports instead of relative on
269
313
270
314
```
271
315
272
-
4. Delete the existing content of the `AppComponent`template (html file of the component) if it is not already done and the variables declared in the `AppComponent` class. Display the `LoginFormComponent` in the `AppComponent`by addding `<app-login-form></app-login-form>` in its template. Don't forget to add the `LoginFormComponent` class to the `imports` array of the @Component definition of the `AppComponent`.
316
+
4. Delete the existing content of the `App` component template (html file of the component) if it is not already done and the variables declared in the `App` class. Display the `LoginFormComponent` in the `App` component by addding `<app-login-form></app-login-form>` in its template. Don't forget to add the `LoginFormComponent` class to the `imports` array of the @Component definition of the `App` class (if you used VSCode aucompletion to add the new component to the App component template, then it should be added automatically).
273
317
274
-
5. Complete the `login-form.component.ts` file: add a field `title = 'Authentication'`. Then, use text interpolation in the template to pass the title from the component ts file to the h1 tag.
318
+
5. Complete the `login-form.ts` file: add a field `protected readonly title = signal('Authentication')`. Then, use text interpolation in the template to pass the title from the component ts file to the h1 tag. Don't forget that you need to invoke a signal to read its value.
0 commit comments