FAQs Angular Maximilian Schwarzmuller

SUPER Technology (SUPER Tech)
6 Feb 202134:19

Summary

TLDRThis video answers common Angular questions around versioning, project setup, debugging techniques, component communication, state management with Redux, deployment, and more. It provides an overview of the Angular framework evolution from AngularJS to modern Angular, recommending using the Angular CLI for project scaffolding. It stresses the importance of reading error messages and using browser developer tools for debugging. For passing data between components, it discusses property binding and RxJS subjects. It also covers persisting state with local storage vs databases and hosting considerations.

Takeaways

  • 😊 Angular 2+ is a complete rewrite of AngularJS 1.x with a different architecture focused on components.
  • πŸ‘ Use the Angular CLI to set up projects - it provides a flexible workflow and optimizations.
  • πŸ’‘ Read error messages carefully - they often provide useful hints on fixes.
  • πŸ”Ž Use source maps and tools like Augury to debug Angular apps.
  • πŸš€ You can use Angular with any backend language like PHP, Node.js etc. through REST APIs.
  • πŸ”€ Use services like RxJS Subjects to communicate between components.
  • πŸ“₯ Persist state across page reloads with local storage or a server database.
  • ☁️ Angular apps can be hosted on static hosts like AWS S3 or Firebase.
  • βš™οΈ Configure your server to return index.html for 404s so client-side routing works.
  • 🌐 Anyone can view client-side JS code, so don't put sensitive data there.

Q & A

  • What was the major reason the Angular team decided to do a complete rewrite and release Angular 2?

    -One of the major reasons was performance issues that could happen in bigger AngularJS apps. Angular 2 focused more on creating single page applications and is highly component driven to address these performance challenges.

  • Why do we have version numbers higher than Angular 2 like Angular 4, 5, etc.?

    -The Angular team adopted semantic versioning which has major releases, minor releases and patch releases. So the version number indicates if there are major new features introduced or just bug fixes.

  • Is it possible to use Angular without the Angular CLI?

    -While theoretically you could do the compilation and bundling in the browser, it is not recommended for performance reasons. The Angular CLI gives a really good and flexible workflow so it is highly recommended to use it.

  • How can data be passed between non-parent/child components in Angular?

    -Services like ones using RxJS Subjects are commonly used to pass data between non-parent/child components. The data can be emitted through the Subject in one component and subscribed to in another component.

  • How can state be persisted across page reloads in Angular apps?

    -Local storage and server-side databases are two main ways. Local storage stores data in the browser across page reloads. A database persists data on a server that the client can access.

  • Can Angular be used with server-side languages like PHP and Node.js?

    -Yes, Angular can be used with any server-side language or framework. Angular runs in the browser and the server provides a REST API that Angular sends requests to and receives responses from.

  • Why do my routes break when deploying an Angular app?

    -The server needs to always return the index.html file for routes to work on refresh and navigate. So the index.html needs to be configured as the 404 document on the server.

  • Can my Angular app code be protected from being visible to users?

    -Unfortunately no. Since the JavaScript code runs in the browser, anyone can view the sources. Information that should not be visible should not be included in the code.

  • How are third-party CSS and JavaScript libraries used in Angular apps?

    -They can be imported via index.html links/scripts or installed via NPM and imported into TypeScript. For JS libraries without types, declare var or declare module can allow use.

  • Why was Angular 2 a complete rewrite and not an update to Angular 1?

    -Angular 2 was built with a different focus on building single page applications and a component driven architecture. It inherited some concepts but is almost entirely new compared to Angular 1.

Outlines

00:00

πŸ˜… Understanding Angular Versions and Terminology

This paragraph explains the history and evolution of AngularJS to Angular, clarifying that Angular 2+ is a complete rewrite and not backwards compatible. It introduces semantic versioning adopted by Angular and explains the meaning of major, minor and patch releases.

05:01

πŸ“ Setting up an Angular Project

This paragraph emphasizes the need to use the Angular CLI for setting up Angular projects, explaining why options like dropping Angular into HTML won't work. It highlights performance reasons for compile-time vs runtime compilation.

10:02

πŸ“‹ Recommended VSCode Extensions for Angular

This paragraph recommends the Angular Essentials extension by John Papa for VSCode, which bundles multiple other useful extensions for Angular development in one easy install.

15:03

πŸ” Debugging Angular Apps

This paragraph provides Angular debugging tips - carefully reading error messages, using console logs, and leveraging source maps in dev tools for run-time debugging with breakpoints.

20:05

πŸ”— Communicating Between Components

This paragraph explains different ways for components to communicate and pass data in Angular - property binding, services with RxJS subjects, and centralized state management with ngrx/redux.

25:06

🌐 Connecting Angular Frontends to Server backends

This explains Angular only handles the frontend SPA, so any backend language/framework can be used as long as it provides a REST API for Angular to interface with.

30:08

πŸ’Ύ Persisting State Across Page Reloads

This paragraph discusses options like local storage or a server database to persist state instead of sessions, since refreshing Angular app resets state.

πŸ“¦ Hosting and Deploying Angular Apps

This paragraph clarifies compiled Angular apps are static files that can be hosted on simple static file hosts, not requiring Node, PHP etc. It also covers configuring routes.

Mindmap

Keywords

πŸ’‘Angular versions

The video discusses the different Angular versions, from AngularJS (Angular 1) released in 2010 to the complete rewrite Angular 2 in 2016 and newer semantic versioning releases up to Angular 6. It explains the backwards compatibility of newer Angular versions and long-term support for older versions like Angular 4.

πŸ’‘Angular CLI

The Angular Command Line Interface is a tool for generating and building Angular projects. The video recommends using the CLI for its active development, optimization capabilities, and flexibility through ejecting. It sets up workflows like compiling, bundling, and minification.

πŸ’‘Debugging

Debugging Angular applications can be challenging. The video advises reading error messages carefully, using console logging, source maps in dev tools to bridge to source code, and Angular-aware tools like Augury for analyzing running apps.

πŸ’‘Component communication

Passing data between Angular components is discussed. Property binding only works parent to child, so services and subjects are better for distant components. NGRX Redux implementation centralizes data flow.

πŸ’‘Server-side code

Angular runs client-side only. Any backend logic on servers to handle security, performance, databases, etc can use any server-side language like PHP, Node, Ruby. It just needs to expose a REST API for Angular.

πŸ’‘State persistance

Angular doesn't maintain server sessions. State must be stored client-side in local storage or server database across page reloads. Local storage is simple key-value storage in the browser.

πŸ’‘Hosting

Built Angular applications produce static files that can be hosted on any simple static file server, not requiring executing server-side code. So specialized static web hosts are sufficient over heavyweight servers.

πŸ’‘Routing issues

Client-side routing with the Angular router instead of server routes can cause issues on refresh. This is fixed by configuring servers to return index.html on 404s so routing can take over.

πŸ’‘Security

The video acknowledges JavaScript code is visible in browsers, so sensitive logic should be kept server-side. Code can be obfuscated but not hidden. Information exposed client-side should not matter if visible.

πŸ’‘Third-party libraries

CSS libraries can be used via CDN imports or CLI config. JavaScript libraries work via script tags, CLI config, or TypeScript imports. The last may require declaring types if definitions are missing.

Highlights

Angular 2 was a complete rewrite of AngularJS 1.x, with a different focus on building single page applications using a component architecture.

Angular adopted semantic versioning, with major, minor and patch releases indicating new features, enhancements or bug fixes.

The Angular CLI provides a flexible workflow for building Angular apps, with capabilities like bundling, minification and optimization.

Use the Angular Essentials extension in VS Code for better autocompletion, syntax highlighting and more when developing Angular apps.

Carefully read Angular error messages for clues on debugging issues, and use console logging and source maps to further inspect application state.

Use services like subjects to communicate between components not in a direct parent-child relationship.

Persist state across page reloads with local storage or a server database, not sessions.

Angular runs client-side only, so use any server-side language like Node or PHP by exposing a REST API.

Add third-party CSS/JS with CDN links, CLI config imports, or script tags in index.html.

Configure servers to return index.html on 404s so Angular router can handle routes.

Angular output is visible in browser, so avoid putting sensitive info in client JS.

Use declare var to add typing info for untyped third-party JS libraries.

Host compiled Angular artifacts on any static file server like S3 or Firebase.

SPA routes don't match servers, so configure 404 fallback to index.html.

Can't hide source code, so ensure publicly exposed JS doesn't reveal secrets.

Transcripts

play00:02

[Music]

play00:06

welcome to this video where i want to

play00:08

dive into some common angular questions

play00:10

now to be honest these are just the

play00:12

questions that came into my mind when i

play00:14

thought about which

play00:16

questions i see a lot if you got other

play00:18

questions

play00:19

please share them in the comments of the

play00:21

video i

play00:22

probably won't be able to answer them

play00:24

all there but i will note them and i

play00:26

might do

play00:26

another video of this kind in the future

play00:28

and with that let's just get started and

play00:30

see which questions i got

play00:35

let's start with one thing that

play00:38

sometimes leads to a lot of confusion

play00:40

the angular version jungle we got

play00:43

angular 2

play00:44

4 5 6 soon at least

play00:47

so what are all these versions about

play00:50

well let's have a look

play00:52

in the past we had angular 1.

play00:56

it was released in 2010 and it was not

play00:59

the first

play01:00

javascript framework manipulating the

play01:03

dom

play01:03

but it still started a little bit of a

play01:06

revolution you could say

play01:08

it suddenly allowed people to create

play01:10

single page applications

play01:12

it made controlling the dom and

play01:14

dynamically rendering content there

play01:16

so much easier and it still also had

play01:20

issues performance could become an issue

play01:23

in bigger apps

play01:24

and this was one of the major reasons

play01:26

why the team decided to do a complete

play01:28

rewrite of angular and release angular 2

play01:31

in 2016. now the road to angular 2 was

play01:35

pretty long

play01:36

a lot of versions were released in

play01:38

between a lot of beta versions

play01:40

but in the end we had a brand new

play01:42

framework that still allows us to create

play01:44

engaging

play01:45

user experiences in the browser but with

play01:48

a different focus

play01:50

angular 2 and all the versions

play01:52

thereafter focus on creating

play01:54

single page applications and they are

play01:56

highly component driven

play01:58

so like in react and so on you build

play02:01

components

play02:02

so the key takeaway here is that angular

play02:05

2 has nothing in common with

play02:07

angular 1. it's a complete rewrite

play02:10

and it inherits some concepts but the

play02:13

majority of things you use in angular 2

play02:15

is brand new

play02:16

or works totally different than it did

play02:19

in angular 1.

play02:20

now what about the other versions now

play02:23

due to

play02:24

that difference in the capabilities

play02:27

we nowadays typically refer to angular 1

play02:29

as

play02:30

angularjs that's the name of the

play02:32

framework nowadays

play02:34

and angular 2 and all versions

play02:36

thereafter

play02:37

are simply referred to as angular just

play02:40

angular speaking of that why

play02:43

do we have more version than just

play02:45

angular 2 because the

play02:47

angular team adopted semantic versioning

play02:49

which means

play02:50

there are major releases minor releases

play02:54

and patch releases

play02:55

so maybe they're just fixing some bugs

play02:58

maybe they're introducing

play02:59

some major new features and the version

play03:02

number indicates that

play03:03

the important thing is angular 3 and 4

play03:06

and so on

play03:06

are not complete rewrites of angular 2.

play03:09

speaking of that we never had an angular

play03:12

3 due to

play03:13

internal version differences with the

play03:15

router and so on

play03:16

so angular 4 was the next release and

play03:18

thereafter we had 5

play03:20

6 and so on now at the point of time

play03:23

recording this

play03:24

angular 6 isn't out yet but new versions

play03:26

will come out

play03:28

every few months due to that semantic

play03:30

versioning

play03:32

and you can actually see a schedule of

play03:35

past

play03:35

and future angular versions on the

play03:38

official angular github repository

play03:40

a link to that schedule can be found in

play03:41

the video description

play03:43

there you see the more recent versions

play03:45

that were released in the past

play03:47

and when we will see angular 6 and 7

play03:50

approximately now again if you look at

play03:52

these versions it looks like

play03:54

everything you learn will be well stuff

play03:57

you don't need once the new version is

play03:58

out

play03:59

and that is not the case the version

play04:02

upgrades

play04:03

are backward compatible and thus far

play04:06

until angular 5. we had no upgrade that

play04:09

introduced

play04:10

any major braking changes at all and

play04:13

there is a long-term version support

play04:15

so version which will receive security

play04:17

patches and so on

play04:18

even if it's a bit older currently

play04:20

that's angular 4.

play04:21

so you can find more information on that

play04:23

page the key takeaway is

play04:25

angular 5 6 7 and so on is the new

play04:28

normality

play04:29

because it's not a new framework every

play04:31

time it's just an update

play04:33

just as we have react 16 17 15 and so on

play04:38

now that we understood the different

play04:39

versions of angular let's have a look at

play04:42

how we create

play04:42

angular projects now chances are you

play04:45

already know this

play04:46

and i'm not going to show you the exact

play04:47

steps here you might know that there is

play04:49

the angular cli you can use

play04:52

the question is do i need dcli do you

play04:54

need to cli

play04:56

i get this question a lot people

play04:59

especially people coming from

play05:00

jquery or angular 1 are used to dropping

play05:04

the angular import into their

play05:06

html files and starting to use angular

play05:09

unfortunately that does not work with

play05:12

angular 2 plus

play05:13

one of the major reasons is that we use

play05:15

typescript instead of vanilla javascript

play05:18

so the language in which we write our

play05:20

code doesn't run natively in the browser

play05:22

obviously we need a compilation step in

play05:25

between and

play05:26

whilst theoretically you could do that

play05:28

compilation

play05:29

in the browser it's not really

play05:31

recommended it's a big performance hit

play05:34

we also have components and templates

play05:36

that use the angular

play05:38

template language this also needs to be

play05:40

compiled

play05:41

by angular so from javascript to

play05:43

javascript but

play05:44

to a different javascript which has a

play05:46

meaning to angular

play05:48

and this could also happen in the

play05:49

browser but again for performance

play05:50

reasons

play05:51

it's recommended to do this on the

play05:53

client so in your on your development

play05:55

machine actually

play05:56

and we also probably want to employ some

play05:59

optimizations like

play06:00

minifying our code and so on this also

play06:02

should be done

play06:03

during development or at the end of it

play06:05

right before shipping the app to

play06:07

production

play06:08

so whilst we do have all these options

play06:10

of running the

play06:11

typescript compilation in the browser or

play06:13

running the angular template compilation

play06:15

in the browser

play06:16

this does not work so we want to do it

play06:19

in the development setup in the

play06:21

development

play06:22

workflow and the same is true for

play06:23

bundling minifying implementing lazy

play06:26

loading and stuff like that

play06:28

now for that we use tools like webpack

play06:31

or

play06:32

typescript of course also minification

play06:35

optimization tools

play06:36

and babel possibly to also transpile

play06:40

um some features though here we use

play06:42

typescript so probably not

play06:44

there are more tools still included in

play06:47

all of that for all the optimizations

play06:49

and all these

play06:50

steps and you can set up such a workflow

play06:53

on your own what you can't do is

play06:55

just drop it into the browser and use it

play06:57

without taking a huge performance hit at

play06:59

least

play07:00

but the easiest way really to get around

play07:04

all of that is to use the cli this cli

play07:07

gives you a really good

play07:08

and flexible workflow you can eject from

play07:12

a cli

play07:13

project to get access to the underlying

play07:15

webpack config

play07:16

so it's not a log in but starting with

play07:20

it

play07:20

creating projects with it and using its

play07:22

capabilities

play07:23

really is a good approach because the

play07:26

cli

play07:26

team which is part of the angular team

play07:28

kind of is

play07:30

actively working on the cli and keeps on

play07:32

improving it

play07:33

to give you ever better angular

play07:35

performance

play07:36

ever better angular apps generated by

play07:39

the cli

play07:40

so i can only encourage you to use it

play07:42

you might not

play07:43

find another github boilerplate

play07:46

that is that actively developed now that

play07:49

we

play07:50

had a look at the cli let's say we

play07:52

opened angular in

play07:53

our editor and let's say that editor is

play07:56

visual studio code

play07:57

i often get asked which extensions i can

play08:00

recommend for it

play08:01

and i might not be the person with the

play08:03

most extensions

play08:05

i am a fan of only using a few and for

play08:08

angular there really is one major

play08:09

extension i want to recommend

play08:11

and that is the angular essentials

play08:13

extension from john popper

play08:16

this is a extension that bundles a lot

play08:18

of other extensions

play08:20

so if you go to the extensions part of

play08:22

visual studio code and you open that

play08:24

extension

play08:25

you see the other extensions it includes

play08:27

and these extensions

play08:29

really make developing angular apps much

play08:30

more fun you get better

play08:32

auto completion you get nice syntax

play08:35

highlighting

play08:35

all that fun stuff with the installation

play08:38

of just that single extension so

play08:40

definitely have a look at that

play08:42

let's leave the setup world and let's

play08:44

have a look at

play08:45

the real development problems we face

play08:48

when creating an

play08:49

angular app and debugging angular apps

play08:52

tends to be one of the hardest things

play08:55

from my experience

play08:56

so from the questions i saw

play09:00

and one thing that really isn't done

play09:03

correctly a lot of the time

play09:04

unfortunately is reading the error

play09:07

messages

play09:08

angular error messages became a lot

play09:11

better

play09:11

and often they give a clear hint about

play09:14

what is wrong or at least where you

play09:16

should look for an error

play09:18

sometimes the error message gives an

play09:20

incorrect error but

play09:22

still points at the correct file and you

play09:24

find another syntax error that

play09:26

led to the incorrect error message so

play09:29

read the error messages and try to

play09:32

incorporate the information you find in

play09:34

there

play09:34

let's have a look at a view here are two

play09:36

error messages

play09:37

the first one says uncaught error

play09:39

template parse errors

play09:41

app product is not a known element

play09:44

you often get that if you forget to

play09:47

declare your custom components

play09:49

so if you didn't add them to the

play09:51

declarations array in app module

play09:54

make sure that you check if that's the

play09:56

case if you did

play09:57

add them to declarations and you use

play09:59

multiple angular modules

play10:01

make sure that they interact correctly

play10:04

with each other

play10:04

that you don't use a component in a

play10:07

module

play10:08

where this hasn't been added to

play10:09

declarations that's another common error

play10:12

source

play10:12

so read these errors and then dive into

play10:16

that part or into that file it's

play10:20

pointing to here in this case to the app

play10:21

product component which somehow seems to

play10:23

be unknown

play10:24

check the selector for typos maybe you

play10:27

have the wrong selector set up in the

play10:29

component decorator of the product

play10:31

component

play10:32

these are the things you should have a

play10:33

look at take these messages seriously

play10:36

and if you can't find a solution google

play10:38

them you find a lot of

play10:40

github issue threads a lot of stack

play10:42

overflow threads

play10:43

for all kinds of common error messages

play10:47

besides reading error messages correctly

play10:50

also use good old console log debugging

play10:53

it's not the best debugging approach in

play10:55

the world

play10:56

and it simply means that you put a bunch

play10:58

of console logs

play11:00

into your code and you log different

play11:02

objects properties

play11:04

things you want to have a look at you

play11:06

see them in the javascript console in

play11:07

the browser

play11:08

and see the values these properties and

play11:11

so on hold

play11:12

at certain points in your app this is

play11:14

not

play11:15

the best way to thoroughly debug an app

play11:18

but to get a quick insight into the

play11:21

state of something into the state of

play11:22

your app

play11:23

at a given point this is a good solution

play11:27

a better solution of course is to use

play11:28

the source maps the cli project gives

play11:31

you automatically

play11:32

source maps are little files the

play11:36

chrome or general the browser developer

play11:39

tools can use

play11:40

to create a bridge from the compiled and

play11:42

optimized

play11:43

javascript code running in the browser

play11:45

to your original files which are of

play11:47

course easier to read for you

play11:49

so use these source maps in conjunction

play11:51

with the browser dev tools

play11:53

let me show you how this works here is a

play11:56

broken angular app and we got an error

play11:58

now let's have a look at the source code

play12:00

from within the dev tools

play12:02

we can simply click on sources here

play12:05

and there in a cli project which creates

play12:08

these source maps i was talking

play12:10

about you should find that webpack

play12:12

folder

play12:13

if you access it in the subfolder in

play12:15

there you should find a folder

play12:18

which in the end replicates your app

play12:20

structure so where you find a source app

play12:23

folder

play12:24

with your actual typescript files

play12:28

and now in these files you can even set

play12:31

breakpoints to stop at certain points of

play12:34

time

play12:34

to get an insight at the value of a

play12:37

certain

play12:38

property for example so this is really

play12:41

something i can recommend

play12:42

having a look at because this is a

play12:44

powerful tool

play12:46

that allows you to take an in-depth look

play12:49

at your app and dynamically debug it

play12:53

with breakpoints with all the features

play12:55

the developer tools gives you

play12:57

in your typescript files even though

play12:59

these are not the files running in the

play13:01

browser so this is really powerful

play13:03

use these tools and you get that for

play13:05

free in a cli project

play13:06

you can just dive into your source code

play13:09

like this

play13:10

there also is another tool besides the

play13:13

already implemented browser developer

play13:15

tools

play13:16

the browser ship with and that's augury

play13:19

that's a chrome

play13:20

extension which you can install from the

play13:22

chrome extension

play13:23

store it's free of course and this is a

play13:26

tool

play13:26

that gives you detailed insight into

play13:29

your app

play13:30

at runtime the difference to the browser

play13:33

developer tools

play13:34

is that augury is aware of the angular

play13:36

architecture and therefore it gives you

play13:38

a good overview over the different

play13:40

components in your app

play13:41

something you don't see easily in the

play13:43

browser because the rendered dom doesn't

play13:45

contain these

play13:46

it gives you access at the properties of

play13:48

your components the state of them

play13:50

it gives you access to injections

play13:53

happening in your app

play13:55

so a detailed look at what's happening

play13:57

at your

play13:58

in your app at runtime being aware

play14:01

of the angular concepts like services

play14:05

like dependency injections on so

play14:07

definitely have a look at this too

play14:08

you access it from the developer tools

play14:10

thereafter and you can simply analyze

play14:13

your page with it as you do with the

play14:15

normal developer tools

play14:17

the ones implemented in the browser so

play14:19

you do with that

play14:20

but augury offers some extra features

play14:22

that are tailored

play14:23

for angular and if you're using ngrx

play14:27

you should also have a look at the redux

play14:29

dev tools which you can connect

play14:31

to ngrx you can find instructions on

play14:34

that

play14:34

on the ngrx documentation and this

play14:37

allows you to have a look

play14:38

into your ngrx store so these are the

play14:42

things i want to bring to your attention

play14:43

to dive deeper into

play14:45

these are a lot of great tools that help

play14:47

you debug

play14:48

your angular apps okay let's leave the

play14:51

world of errors

play14:53

and let's have a look at something else

play14:54

i get asked a lot how can i pass data

play14:56

from component a to component b

play14:59

so here is a concrete setup with the app

play15:01

component a product component and a

play15:03

product

play15:04

component now one easy way to pass data

play15:07

around

play15:07

is by property binding with that input

play15:10

and so on you pass data from

play15:11

app component to products component and

play15:13

maybe then data from

play15:14

products to product component property

play15:17

binding

play15:18

is only possible from parent to child

play15:20

component so only one level at a time

play15:23

therefore reaching out from app

play15:25

component to product component

play15:27

is not possible if you wanted to do that

play15:30

you would use a service instead a

play15:32

service in between

play15:34

possibly or typically to be honest

play15:37

with the rxjs subject which is like an

play15:40

observable but one where you can emit

play15:42

events on your own

play15:43

with next so where you can emit an event

play15:46

let's say from the app component

play15:47

and where you then subscribe in some

play15:49

other place of your app and

play15:50

that place could also be a service by

play15:52

the way or something like that

play15:54

now here it would be the product

play15:56

component and all of a sudden we have a

play15:58

connection from app component to product

play16:00

component

play16:00

through the service and the subject and

play16:03

this also works the other way around

play16:05

we can emit our custom events to pass

play16:07

data up

play16:08

from a child to a parent component but

play16:11

emitting an event from product component

play16:13

to app component directly is not

play16:14

possible

play16:15

but again we can use a service with a

play16:18

subject to emit an event

play16:19

and listen to it in another component so

play16:22

this is how we can communicate between

play16:24

components

play16:24

and pass data around now important if

play16:27

you're using rxjs subjects

play16:29

don't forget to unsubscribe in the

play16:31

components where you do subscribe in

play16:33

ng on destroy to make sure you're not

play16:36

running into memory leaks

play16:38

so you need to store that subscription

play16:39

you're creating with the subscribe

play16:41

method in some property

play16:42

and unsubscribe from that in the ng

play16:44

ondestroy lifecycle hook

play16:47

and let me all that there is one other

play16:49

way of communicating and passing data

play16:51

around

play16:52

and that is using ngrx which is

play16:54

basically a redux

play16:56

implementation for angular apps you want

play16:59

to use

play16:59

angular with a server-side language

play17:02

because angular runs in the browser

play17:04

so a lot of the logic you want to

play17:05

execute can't be executed there because

play17:08

it needs to run

play17:09

on a server for security reasons for

play17:11

performance reasons

play17:12

or because you can't perform that action

play17:14

in the browser like accessing files

play17:16

or because you want to store data in a

play17:18

server-side database

play17:20

now i often get asked can i use angular

play17:22

with php with larawel which is a php

play17:25

framework

play17:26

with node with ruby with anything and

play17:28

the answer is yes you can

play17:31

angular in the end creates the

play17:33

client-side app

play17:34

a single page application running in the

play17:37

browser

play17:38

it doesn't care about your server

play17:40

because it doesn't run there

play17:42

so you can build your server-side

play17:44

back-end with any language and framework

play17:46

you

play17:47

want angular is only good at creating

play17:50

spas

play17:51

so it only creates that it only

play17:54

runs that and your server-side back-end

play17:58

now needs to be a restful api so

play18:01

not a back end where you render views

play18:03

like with laravel's plate templating

play18:05

engine you're not doing that

play18:07

angular is responsible for the front end

play18:09

your backend is a restful service

play18:12

sitting there providing api endpoints to

play18:14

which angular can send requests

play18:17

through its http client and

play18:20

where it also of course receives

play18:21

responses and on the server you can then

play18:24

implement any logic

play18:25

including a database if you want that

play18:28

you want

play18:28

you communicate to angular through the

play18:31

restful

play18:32

api another question i get a lot

play18:35

is can i use redux and i already kind of

play18:38

answered this earlier in this video

play18:40

yes you can with ngrx now

play18:44

redux solves one problem that we have a

play18:47

lot of components communicating with

play18:49

each other and which

play18:50

with services and we get a lot of

play18:54

complex interactions that become hard to

play18:56

maintain especially in bigger apps of

play18:58

course

play18:59

redux solves this by offering us a redux

play19:02

store so one central place in the app

play19:05

where our data is stored

play19:06

and where we can connect our different

play19:09

parts of the app too

play19:10

so components can then receive data from

play19:13

the store and dispatch actions to the

play19:15

store

play19:16

and we have a clear flow of data and a

play19:18

central place where everything gets

play19:20

stored

play19:21

and that is redux and we can use it in

play19:23

angular

play19:24

since redux isn't tied to react you

play19:27

could implement redux just like that in

play19:29

angular

play19:30

or you use a specific implementation of

play19:32

it called

play19:33

ngrx now this is not a must as i said

play19:36

you can implement the normal redux

play19:38

you're on your own

play19:39

but you can also use ngrx to get an

play19:42

approach that nicely fits into the

play19:44

angular world

play19:45

and follows many of the concepts angular

play19:47

already follows

play19:48

so ngrx is something you want to google

play19:51

and you want to learn

play19:52

if you want to implement redux in your

play19:54

angular apps

play19:55

speaking of state i also see the

play19:58

question

play19:58

my state is lost after i refresh the

play20:01

page

play20:02

and with that people mean that unlike on

play20:04

traditional web apps where you often

play20:06

have sessions to manage the user state

play20:08

on the server

play20:09

and therefore if the user refreshes the

play20:11

page he's still logged in and so on

play20:14

and client-side apps this is not the

play20:16

case if you refresh your angular app

play20:19

it really resets it doesn't keep the old

play20:22

state

play20:23

because no server is involved you might

play20:26

have that restful api you are sending

play20:28

data to

play20:29

but that is a stateless server it

play20:31

doesn't store

play20:32

anything any session information from

play20:35

your client

play20:36

so you have two ways of storing state

play20:39

across page reloads then

play20:41

you can work with local storage which

play20:44

is a client-side storage storage in the

play20:46

browser

play20:47

or with a server side database you're

play20:50

still not going to be using sessions

play20:52

then

play20:52

but you can persist some general user

play20:55

data there of course

play20:56

like the orders of a user stuff like

play20:58

that for

play20:59

session-like approaches you would have a

play21:01

look at local storage though

play21:03

so local storage runs on a client so

play21:05

it's not fully under your control

play21:07

with that i mean that if the user clears

play21:10

the cache manually

play21:11

uninstalls the browser or something like

play21:13

that this is of course gone but the same

play21:16

is true for cookies i guess

play21:18

since they're also living on a client

play21:21

it's accessible by javascript only

play21:23

so there's a good and a bad thing bad

play21:25

thing is if your page is vulnerable

play21:27

to cross-site scripting attacks by

play21:30

default

play21:30

angular should give you a good

play21:32

protection against that though

play21:34

if it is vulnerable then people can

play21:36

access your local storage

play21:37

and possibly extract json web tokens

play21:40

from there

play21:41

on the other hand it's not vulnerable to

play21:44

csrf attacks for example so that is the

play21:48

upside of that

play21:49

it's a simple key value story that's all

play21:51

important to keep in mind

play21:52

that means that well you can't store

play21:55

complex data in there you can't store

play21:57

images in there really just simple data

play22:00

but for

play22:01

keeping track of a user session for

play22:03

example of a

play22:04

json web token and so on this is perfect

play22:07

now the server side database is better

play22:10

for

play22:11

data that needs to be under your control

play22:13

like

play22:14

the user itself the user data you store

play22:17

stuff like that it is accessible from

play22:20

the server only so the client can't

play22:22

access it which of course is an extra

play22:24

security layer

play22:25

and of course you can use any database

play22:27

you want and you can store whichever

play22:29

data and data types you want there for

play22:31

and this is how you persist state or how

play22:34

you store

play22:35

data there is no session you do use

play22:38

these two things you can also use

play22:40

cookies i should add

play22:42

but you don't use sessions that's the

play22:44

key takeaway there is no session stored

play22:46

on the server and no session keys stored

play22:49

in a cookie on the client

play22:51

can i host my angular app on hiroko and

play22:54

so on that's another question i see a

play22:55

lot

play22:56

let's understand how hosting an angular

play22:59

application works

play23:01

if we use the cli or any other build

play23:03

process

play23:04

in the end once we're done we're going

play23:06

to build

play23:07

our project which means we generate a

play23:10

bunch of build artifacts

play23:11

which is the index.html file and a lot

play23:14

of javascript files typically

play23:16

the bundled and optimized javascript

play23:19

files that make up our app so these

play23:22

are the build artifacts we might of

play23:24

course also have some style files

play23:27

these need to be shipped to a server and

play23:30

these are just static files if you have

play23:32

a look at that

play23:33

there's no server set code there's no

play23:35

php file the javascript files are not

play23:38

going to contain any node.js code

play23:40

so there's no need to execute code on a

play23:43

server

play23:44

and therefore a static host like aws

play23:48

s3 or firebase hosting is all you need

play23:52

a static host is basically a file

play23:54

storage that also can accept

play23:57

http requests and return your files but

play23:59

where you don't run

play24:00

any server-side code so can you host

play24:03

your app on hiroko

play24:05

yes you can but since hiroko essentially

play24:09

requires you to create a app with some

play24:12

server side language

play24:13

you create a node.js app just to serve

play24:16

the index.html file in the end and the

play24:19

same might be true for other languages

play24:21

so that might be overkill google for

play24:24

static web hosts instead

play24:25

to find fitting hosts for angular apps

play24:29

speaking of deployment another question

play24:31

i see a lot is

play24:32

my routes don't work after deployment so

play24:35

here's the setup

play24:36

we got the user you and the server

play24:39

you enter a url in the browser and your

play24:42

angular app loads a page with your

play24:45

angular uploads i should say

play24:47

you then navigate to the products page

play24:50

there by clicking on a link

play24:52

and that page loads this is done through

play24:55

the angular router

play24:56

because there you register the different

play24:59

pages and since you only have one

play25:01

real page the index.html file it's not

play25:04

really loading a new file from the

play25:06

server

play25:07

angular just re-renders your page to

play25:10

give you the illusion of loading a new

play25:12

page

play25:13

and it can do that because it

play25:14

essentially listens to the clicks

play25:17

on your links when you use the router

play25:19

link directive

play25:20

and well triggers the angular router

play25:22

your server doesn't have any routes

play25:26

if you refresh the page now you get a

play25:28

404

play25:29

error because if you refresh the page

play25:32

the angular router has no chance of

play25:34

taking over

play25:35

because refreshing means you send a new

play25:37

request to the server

play25:38

and since you have no routes there

play25:40

except for the default route which

play25:41

returns the index.html file

play25:43

well you get an error the solution is

play25:47

you need to always load the index.html

play25:50

file so on your server you need to

play25:52

define the index.html file

play25:54

as your 404 error document

play25:58

this then allows the router of angular

play26:01

to take over and handle that request

play26:03

and possibly find the route and then

play26:06

render the correct page

play26:08

and if you want to have a 404 error page

play26:11

for routes you don't handle in the

play26:13

angular app

play26:13

either you would need to set it up in

play26:16

angular still

play26:17

here's another question i get a lot

play26:19

everyone can see my code

play26:21

it's true everyone can see your code if

play26:24

you go into the browser and

play26:26

open an angular app and you go to that

play26:28

sources tab as i showed you earlier in

play26:30

this video

play26:32

well you can inspect the code and

play26:34

everyone can do that you can do that for

play26:36

other apps and other people can do it

play26:38

for your app

play26:39

you can't protect against it javascript

play26:41

runs in the browser

play26:43

it's not compiled not pre-compiled so

play26:46

it's visible to everyone

play26:47

and you can't do anything about that the

play26:49

only thing you can do is

play26:51

be aware of that and don't put sensible

play26:53

information

play26:54

in there make sure you only have code

play26:58

and information in that javascript file

play27:01

that

play27:01

doesn't really matter if other people

play27:03

can read it you can make it harder for

play27:06

people to read it

play27:07

but you can't prevent it i also often

play27:10

see the question

play27:11

how you can use third-party css

play27:14

libraries

play27:15

like bootstrap and angular apps and

play27:17

there are two major approaches

play27:19

the first one is simple you throw a link

play27:22

ref in your index.html file

play27:24

either pointing to a local file you

play27:26

downloaded or to a file hosted on some

play27:29

cdn

play27:30

the second possible approach is to

play27:33

either

play27:34

download the css file the css library

play27:37

file you want to use

play27:38

or use npm install library to install it

play27:42

and then add an entry to the styles

play27:45

array

play27:45

in the angular cli.json file

play27:48

and that entry simply needs to be the

play27:51

path

play27:52

seen from the source folder on so not

play27:55

from the root folder but from the source

play27:57

folder of your add-on

play27:59

to that file you downloaded or you

play28:01

installed

play28:02

this is how simple you can add a css

play28:05

library

play28:06

and the same for third-party javascript

play28:08

libraries

play28:10

you have three possibilities here you

play28:13

can put a script source

play28:14

import in index.html

play28:18

and you can also point to some cdn here

play28:21

you can use npm install or download the

play28:24

file and add an entity to scripts array

play28:27

or you do the same but directly import

play28:31

it

play28:31

into your typescript files by adding an

play28:33

import statement at the top

play28:35

you can of course import other

play28:36

typescript files and you do that all the

play28:38

time in angular apps

play28:40

well turns out you can also just import

play28:42

other javascript files

play28:44

so that is a perfectly fine alternative

play28:46

then you don't have to add an entry to

play28:48

scripts

play28:49

to the scripts array in the angular cli

play28:51

json file because if you just add an

play28:53

import statement in one of your

play28:55

typescript files

play28:56

in the end this imported javascript file

play28:59

will be included

play29:00

in the final bundle so then you got it

play29:03

in your application too

play29:05

one important note though since you're

play29:07

using typescript in angular

play29:09

there are javascript packages that might

play29:11

be unknown to typescript

play29:13

because they don't ship with a so-called

play29:16

types declaration file

play29:18

a file that basically translates the

play29:20

javascript

play29:22

functionalities to typescript if your

play29:26

downloaded library doesn't have such a

play29:28

file

play29:29

you have to add something to the

play29:33

sourcetypings.dts file you should find

play29:35

in your angular projects

play29:36

or which you can create if you don't

play29:38

have it and there you add that declare

play29:41

module

play29:41

and then your library name line here

play29:45

and thereafter when importing it in

play29:47

other javascript files

play29:49

typescript should understand the

play29:51

features of that library

play29:53

you need to add an import statement then

play29:56

though

play29:57

with the scripts array approach this

play29:59

will not work

play30:00

there you would instead need to add

play30:02

declare war

play30:04

and then any object you use from a

play30:06

library of type any

play30:08

at the top of the typescript file where

play30:11

you want

play30:11

to use it let me show you an example

play30:14

here is a very simple project i created

play30:17

and now in there i will install a new

play30:20

library with the npm install

play30:22

and i will install lowdash which is a

play30:24

library providing a couple of utility

play30:26

functions to javascript

play30:28

i'll add dash dash save to download and

play30:30

install it

play30:31

and also add an entry in the

play30:33

package.json file

play30:35

now once this is finished let's go to

play30:38

node modules

play30:39

and let's have a look at what we got so

play30:42

here's lowdash and if we open it

play30:44

we see a bunch of javascript files in

play30:46

there because it's

play30:47

shipped in a way that we can import just

play30:49

what we need and we don't have to import

play30:51

all the files

play30:55

one thing that's missing though or

play30:56

should be missing at least is such a dts

play30:59

file

play31:00

so what can we now do how can we use low

play31:03

dash

play31:04

for low dash and for a lot of javascript

play31:07

libraries to be honest

play31:08

a good approach would be the following

play31:10

one let's say we want to use it here

play31:12

in the constructor of our app component

play31:15

there i will create a new array

play31:18

one two three and i want to shuffle that

play31:21

and low dash has a function for that

play31:24

so normally i can use the low dash

play31:26

symbol the underscore

play31:28

and call shuffle and pass my array

play31:32

and i could output the result of that

play31:34

operation

play31:35

now if i do that and i run ng-surf

play31:39

we already can see it here in the file

play31:41

that this underscore isn't recognized

play31:43

typescript doesn't know and therefore

play31:46

during building it gives me an error it

play31:49

shows me that it

play31:50

can't find that name now we can

play31:54

import low dash so we can import

play31:58

low dash like this and this would be the

play32:00

correct import statement but

play32:02

still it would fail now what i said is

play32:06

that you can now declare

play32:07

a var underscore so the symbol you're

play32:10

going to use

play32:11

and of type any so that you're allowed

play32:13

to call anything

play32:14

in that typescript file and now the

play32:16

error is gone because now we're telling

play32:18

typescript

play32:19

it's okay i know that this symbol is

play32:21

available

play32:22

and you don't need to care about what i

play32:24

can do with it

play32:26

and now if we go back to our running

play32:28

application

play32:29

and i reload the page we see the

play32:32

shuffled array so we're using low dash

play32:35

here

play32:35

we're importing it like this and we

play32:37

added declare war

play32:39

and the alternative i mentioned would be

play32:42

to go to the typings dts file

play32:44

and add declare module

play32:48

low dash like this

play32:52

with that it should also work if i save

play32:54

this and here in the app component file

play32:56

i removed the declare bar low dash thing

play33:01

and now i can import underscore from low

play33:03

dash

play33:04

so this is now an alternative and with

play33:06

that

play33:07

it still works well

play33:11

here it now gives us a shuffled result

play33:14

and

play33:14

this is another way of doing this here

play33:17

basically using that import statement

play33:20

where i say

play33:20

i'm importing a specific symbol from

play33:22

that file so these are

play33:24

the common approaches you use for using

play33:27

javascript packages or

play33:29

files libraries a lot of libraries

play33:32

already ship with these dts files though

play33:35

and have

play33:35

es6 imports and exports so there you

play33:39

just import something from that library

play33:41

and you don't need to declare the module

play33:43

you can basically find it out by

play33:45

checking out the github repositories

play33:47

of whichever libraries you're going to

play33:49

use

play33:50

and that was basically it i hope you

play33:52

enjoyed it i hope this was helpful

play33:54

share any other common questions you see

play33:57

you know

play33:58

in the video comments and i will see

play34:00

what i can do and i hope to see you

play34:02

again in future videos

play34:04

on my academic channel so see you then

play34:17

bye