Get a copy, not access directly,
👉 Spread operator. (
...var)- Don't duplicate tasks.
- Access data and used in somewhere.
- Just another class which help centralize your codes.
Why we need services? If we don't use, just use what we have (binding, emit,...) → more components and they need to communicate to each other → too complicated!
- Naming:
logging.service.ts
- There is NO DECORATOR like @Service() → just a normal typescript class!
DI injects class (of a service) into our component automatically. ← we need to inform angular that we need to add this instant → add a constructor
Service: store and manage our data → exchange property & event binding → get event to app component.
Without account services, we have to emit & output our data and event (add acc for example). However, with services, we don't need them anymore, just inject the service and put the account into it!
Inject a service to father → all its child component get the same instance of the service! ⇒ only go down in the tree components → if there is a duplicate in child, it will overwrite the father's.
[video] If don't want create A NEW INSTANCE IN CHILD (which will overwrite the one coming from father) → just remove the service in
providers! → it will use the service of the father.- Normally, if service A doesn't contain (inject) any service → no need
@Injectable()
- If we wanna inject service B into service A → we need to add
@Injectable()into A (not B!!!!)
GOOD PRACTICE: ALWAYS ADD
@Injectable() for all services!With services, we don't have to build complex inputs, outputs chanes where you pass events and properties to get data from component A to B,... → much cleaner!
👉 Example: exchange active / inactive users.
👉 Project with recipes and shopping-list. (change from using EventEmitter to service) — videos
👉 Project with recipes and shopping-list. (change from using EventEmitter to service) — videos
When we use
.slice() to copy a list (to work on this), there may be some event cannot access the original one (eg. when using addIngredients) → we need to emit an event containing the original list.Angular ships its own router which allows to change URLs of our application.
Where? ⇒ Because the router controls URLs of all things in our apps, the place we can put it is in
app.module.tsAdd some links to app (video)
(video) Why we need
"/" before "/servers"? → if we on home page, it't normal, but if we in subpage (eg. /servers), if there is another link to "servers" (eg. <a routerLink='servers'>), it will be "/servers/servers" ← error!!!We can relative / absolute paths inside
routerLink.We use
<a class="active"> for current tab. ← how to use angular to add/remove this class auto?(video)
routerLinkActive will check if the current path contains the path given in routerLink ← the empty path, ie. "/" is in all paths!!! → this may lead to a problem in which "Home" tab is always "active" ⇒ we need routerLinkActiveOptionsPerform navigation after users click on some button, for example.
If we want
.navigate() knows where we are,(Videos: Passing params to Routes + Fetching Route params + fetch reactively) For example we wanna navigate to users, each user a path → change with ids. ←
id will be the param of the Route.Get the
id from the router,GOOD PRACTICE: Always
.unsubscribe() the observable has been created!How to retrieve the informatoin from the URLs? ⇒ check video.
(video + codes) No need to change to a new page for each child component, just display them on a sidebar (a part of the view) when we click on the navigator.
Before enter to edit-server, there is
?allowEdit=1, however, after navigate to edit-server, this info is gone. How to preserve it?For example: building 404 page.
Error of
path: '' (nothing) ← default behavior to check in angular is "prefix" (check from beginning) → every urls contains this "nothing" ('')Store all routing tasks in a single file. → no need to stored in
app.module.ts.👉 Example file.
(video) Create
auth-guard.service.ts (file) containing the service to control the authentication. → use CanActivate ← angular executes this before router loaded!Example:
auth.service.ts → a fake service for the testing. In real app, we use this file to get the info / confirmation from the server about the authentication!Apply to routes?
(video) Show the father route list, only protect child routes? → use
CanActivateChild(video + file) Control whether you are allowed to leave a route or not ← Confirm to leave the input/changes!!! solve the problem of user accidentially navigating away!!!
Idea: angular router can execute
canDeactivate() in a service (can-deactivate-guard.service.ts) > component we are currently on has canDeactivate() ← how guard communicates with our components.(video + codes) Display the component after fetch from the server (async data) → should watch the videos + below notes:
(video) When deployment, all URLs are parsed by the server (which hosts your app) first → then angular → route of angular (eg. nagivate something strange page to not found) may not work like on localhost. → need to make sure your web server return html file you want!
Hash mode routing → informs your web server on care the part of URL before "#"! → all things behind will be ignored by webserver.