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
π Codes for this section.
Service: store and manage our data β exchange property & event binding β get event to app component.
π Codes for this section.
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!
π Codes for this section.
π 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.π Codes for this section.
Get the
id from the router,GOOD PRACTICE: Always
.unsubscribe() the observable has been created!π Video for this section.
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.