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/en/services.md
+22-32Lines changed: 22 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,9 +59,9 @@ Dependencies can be provided at three levels:
59
59
60
60
## Practical Work: State management
61
61
1. Generate an `AuthenticationService` with the CLI in the `app/services` folder
62
-
2. Move the `loggedIn` logic from the `AppComponent` to the service
62
+
2. Move the `loggedIn` logic from the `App` component to the service
63
63
3. Inject the service in the `LoginFormComponent` and use it.
64
-
4. Implement a logout method in the authentication service and add a logout button in the `AppComponent` that calls it and navigates back to the `LoginFormComponent`. Here is the html and css:
64
+
4. Implement a logout method in the authentication service and add a logout button in the `App` component that calls it and navigates back to the `LoginFormComponent`. Here is the html and css:
65
65
66
66
<CodeGroup>
67
67
<CodeGroupItemtitle="app.html">
@@ -154,7 +154,7 @@ export class UserService {
154
154
<CodeGroupItemtitle="Component">
155
155
156
156
```ts
157
-
import { Component, inject } from'@angular/core'
157
+
import { Component, inject, signal } from'@angular/core'
7. Refactoring is also needed to keep the `authenticationGuard` working. Make the `loggedIn`boolean in the `AuthenticationService`depend on a `token` field and make the `LoginFormComponent` save the token that it gets from the login call in that field.
356
-
You will also need to refactor the logout to empty the `token`.
358
+
7. Refactoring is also needed to keep the `authenticationGuard` working. Make the `loggedIn`variable in the `AuthenticationService`a computed signal that depends on the `token` signal.
359
+
You will also need to refactor the logout to empty the `token` signal value.
8. Add a register button next to the login button in the `LoginFormComponent`, give it the attribute `type="button"` so that Angular knows it is not this button that triggers the `ngSubmit` event on the form and make it call the register method.
384
371
You should now be able to register a user and login.
385
-
If you are having trouble, check the errors in the network tab of the developer tools (preview tab of the network call in error), your email or password may not comply with the policy.
372
+
If you are having trouble, **check the errors in the network tab** of the developer tools (preview tab of the network call in error), your email or password may not comply with the policy.
386
373
387
374
<CodeGroup>
388
375
<CodeGroupItemtitle="HTML">
@@ -405,7 +392,7 @@ If you are having trouble, check the errors in the network tab of the developer
405
392
</CodeGroupItem>
406
393
</CodeGroup>
407
394
408
-
9. It is time to handle errors. The subscribe method can be passed an object that takes three callbacks: a *next*, an *error* and a *complete* (we will look at this in more details in the next chapter). Declare an `errorMessage`field on the `LoginFormComponent` and update it with the error information retrieved from the argument of the `error` callback. Display the error message on the form. Check that the error message is actually shown when you login with incorrect credentials.
395
+
9. It is time to handle errors. The subscribe method can be passed an object that takes three callbacks: a *next*, an *error* and a *complete* (we will look at this in more details in the next chapter). Declare an `errorMessage`signal in the `LoginFormComponent`class and set it with the error information retrieved from the argument of the `error` callback. Display the error message on the form. Check that the error message is actually shown when you login with incorrect credentials.
12. Make changes to the `FilmSearchComponent` to call this new service with the title filled in by the user, save the response to the `films` field in the `FilmSearchComponent`.
462
+
12. Make changes to the `FilmSearchComponent` to call this new service with the title filled in by the user, save the response to the `films`signal field in the `FilmSearchComponent` class.
476
463
477
464
13. Check that the token is sent as a HTTP header via the developer tools of your browser.
478
465
479
466
::: details Expected result
480
467

481
468
482
469

483
-
:::
470
+
:::
471
+
472
+
## To go further: HttpResource
473
+
The `HttpClient` makes use of Observables from the `rxjs` library. Having two reactivity models (Observables and signals) competing in the same framework may seem strange. Observables have been around since the first version of Angular and are mainly encountered whilst making http calls or interacting with reactive forms, they were not necessary everywhere thanks to `zone.js`. They shine in complex reactive use cases that don't form the bulk of an Angular application code base but that are essential to the proper running of your application. Signals won't take over the role of Observables as they offer a less powerful API, however they will replace Observables in many simpler cases. There's a signal-based wrapper for the `HttpClient` called `HttpResource` that was introduced in Angular 19.2 as experimental. You can explore its capabilities [here](https://angular.dev/guide/http/http-resource). It is advised to only use it for GET requests.
Copy file name to clipboardExpand all lines: docs/src/fr/services.md
+22-32Lines changed: 22 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,9 +59,9 @@ Les dépendances peuvent être fournies à trois niveaux :
59
59
60
60
## TP : Gestion de l'État
61
61
1. Générez un `AuthenticationService` avec le CLI dans le dossier `app/services`
62
-
2. Déplacez la logique du `loggedIn`de l'`AppComponent` vers le service
62
+
2. Déplacez la logique du `loggedIn`du composant `App` vers le service
63
63
3. Injectez le service dans le `LoginFormComponent` et utilisez-le.
64
-
4. Implémentez une méthode de déconnexion dans le service d'authentification et ajoutez un bouton de déconnexion dans l'`AppComponent` qui l'appelle et provoque une navigation vers le `LoginFormComponent`. Voici l'html et le css :
64
+
4. Implémentez une méthode de déconnexion dans le service d'authentification et ajoutez un bouton de déconnexion dans le composant `App` qui l'appelle et provoque une navigation vers le `LoginFormComponent`. Voici l'html et le css :
65
65
66
66
<CodeGroup>
67
67
<CodeGroupItemtitle="app.html">
@@ -154,7 +154,7 @@ export class UserService {
154
154
<CodeGroupItemtitle="Component">
155
155
156
156
```ts
157
-
import { Component, inject } from'@angular/core'
157
+
import { Component, inject, signal } from'@angular/core'
7. Une refactorisation est également nécessaire pour que l'`authenticationGuard` continue de fonctionner. Faites en sorte que le booléen `loggedIn` dans `AuthenticationService`dépende d'un champ `token` et faites en sorte que le `LoginFormComponent` sauvegarde le token qu'il obtient de l'appel de connexion dans ce champ.
357
-
Refactorez aussi la méthode `logout()` pour qu'elle remette le`token` à null.
359
+
7. Une refactorisation est également nécessaire pour que l'`authenticationGuard` continue de fonctionner. Transformez la variable `loggedIn` dans l'`AuthenticationService` en signal computed qui dépend du champ `token`.
360
+
Refactorez aussi la méthode `logout()` pour qu'elle remette la valeur du signal`token` à null.
8. Ajoutez un bouton d'enregistrement à côté du bouton de connexion dans le `LoginFormComponent`, donnez-lui l'attribut `type="button"` afin qu'Angular sache que ce n'est pas ce bouton qui déclenche l'événement `ngSubmit` sur le formulaire et faites-lui appeler le méthode d'enrôlement.
385
372
Vous devriez maintenant pouvoir enregistrer un utilisateur et vous connecter.
386
-
Si l'enrôlement semble ne pas fonctionner, vérifiez l'onglet network des outils de développement de votre navigateur (onglet preview de l'appel réseau en erreur), votre mail ou mot de passe ne respectent peut-être pas les règles de validation du back.
373
+
Si l'enrôlement semble ne pas fonctionner, **vérifiez l'onglet network des outils de développement de votre navigateur** (onglet preview de l'appel réseau en erreur), votre mail ou mot de passe ne respectent peut-être pas les règles de validation du back.
387
374
388
375
<CodeGroup>
389
376
<CodeGroupItemtitle="HTML">
@@ -406,7 +393,7 @@ Si l'enrôlement semble ne pas fonctionner, vérifiez l'onglet network des outil
406
393
</CodeGroupItem>
407
394
</CodeGroup>
408
395
409
-
9. Il est temps de gérer les erreurs. La méthode subscribe peut prendre un objet qui propose trois callbacks: une *next*, une *error* et une *complete* (nous verrons cela plus en détail dans le chapitre suivant). Déclarer un champ`errorMessage`sur le `LoginFormComponent` et le mettre à jour en vous servant de l'argument renvoyé par la callback `error`. Afficher le message d'erreur sur le formulaire. Vérifier que le message d'erreur s'affiche bien lorsqu'on saisit des identifiants incorrects.
396
+
9. Il est temps de gérer les erreurs. La méthode subscribe peut prendre un objet qui propose trois callbacks: une *next*, une *error* et une *complete* (nous verrons cela plus en détail dans le chapitre suivant). Déclarer un signal`errorMessage`dans la classe `LoginFormComponent` et changer sa valeur en vous servant de l'argument renvoyé par la callback `error`. Afficher le message d'erreur sur le formulaire. Vérifier que le message d'erreur s'affiche bien lorsqu'on saisit des identifiants incorrects.
error: errorResponse=> { /*insérer du code ici*/ }
426
413
})
427
414
```
428
415
</CodeGroupItem>
@@ -473,12 +460,15 @@ const options = {
473
460
}
474
461
```
475
462
476
-
12. Apportez des modifications au `FilmSearchComponent` pour appeler ce nouveau service avec le titre renseigné par l'utilisateur, enregistrez la réponse dans le champ`films`du`FilmSearchComponent`.
463
+
12. Apportez des modifications au `FilmSearchComponent` pour appeler ce nouveau service avec le titre renseigné par l'utilisateur, enregistrez la réponse dans le signal`films`de la classe`FilmSearchComponent`.
477
464
478
465
13. Vérifiez que le token est envoyé sous forme d'en-tête HTTP via les outils de développement de votre navigateur.
479
466
480
467
::: details Résultat attendu
481
468

482
469
483
470

484
-
:::
471
+
:::
472
+
473
+
## Aller plus loin : HttpResource
474
+
L'`HttpClient` utilise les Observables de la librairie `rxjs`. Avoir deux modèles de réactivité (les Observables et les signaux) qui coexistent au sein du même framework peut sembler étrange. Les Observables sont présents depuis la première version d'Angular et sont principalement rencontrés lors d'appels réseaux ou d'interactions avec les formulaires réactifs. Ils excellent dans des cas d'utilisation réactifs complexes qui ne constituent pas la majeure partie de la base de code d'une application Angular, mais qui sont essentiels au bon fonctionnement de votre application. Les signaux ne prendront pas le rôle des Observables car ils offrent une API moins puissante, toutefois ils remplaceront les Observables dans de nombreux cas plus simples. Un wrapper du `HttpClient` basé sur les signaux a été introduit en expérimental en Angular 19.2, le `HttpResource`. Vous pouvez explorer ses capacités [ici](https://angular.dev/guide/http/http-resource). Il est conseillé de ne l’utiliser que pour les requêtes GET.
0 commit comments