◀ prev
▶ next
Up to speed in 15 or 60 minutes

I CODE DREAMS™
Angular is a platform that makes it easy to build applications with the web. Angular combines declarative templates, dependency injection, end to end tooling, and integrated best practices to solve development challenges.
Angular empowers developers to build applications that live on the web, mobile, or the desktop
I recommend using Visual Studio Code or WebStorm with Angular CLI.
- Node.js >= 18.19.1 or newer
- NPM >= 3 (comes with Node.js)
Open up a terminal and execute this command and wait...
$ npm install --global @angular/cli
ng
command away!
In a terminal:
Surf to
localhost:4200
and shiver!
Serve and hot reload
Modify app.component.ts
or
app.component.html
to see hot reloading in action.
Use Control + C to interrupt the web
server, go on with your life and start creating a component...
1. Create a component
In a terminal:
This will result in a set of files which make up your component.
page.component.html
- templatepage.component.ts
- component classpage.component.css
- styling (scoped!)page.component.spec.ts
- unit test
2. Use the newly created component
First run ng serve
again to take advantage of hot
reloading.
Open up app.component.html
and replace the content
with this code:
<app-page></app-page>
This should result in your browser displaying:
Page works!
Trouble shooting
If the previous rendered void:
-
Have a look at the output of
ng serve
in the terminal. - Open up the developers console of your browser and look at the console log.
Note about hot reloading
After creating a component (or generating a service), run
ng serve
again and reload the application in your
browser.
Model binding
Add a property to your component:
// page.component.ts
class {
public message: string = 'Hello, world!'
// ...
}
<!-- page.component.html -->
{{ message }}
You can apply this concept to HTML attributes, classes and styles as well!
Model binding - Two way(s)
Use NgModel.
<input [(ngModel)]="name">
<button (click)="submitForm()">Submit</button>
See also Tour of Heroes.
Services
Where components are for view logic, services come into place for other logic. Reusable and the such.
Inject the service in the constructor using dependency injection:
constructor(private myService: MyService) {}
The service is now available as this.myService
and
methods can be called on it.
Standard services
constructor(private http: HttpClient) {}
Angular philosophy
- Many very small components
- Services for logic
- Modules
Read more in the Angular Style Guide.
Minimal Angular Material application with gesture support
TODO what is Material (design)?
Add Material Design
Optional packages if you like!
Touch gesture support
Add touch gesture support with hammerjs
Add import to
main.ts
import 'hammerjs'
Material Design Components
Use .mat-typography
for global typography.
// page.component.ts
import { MatButton } from '@angular/material';
...
<!-- page.component.html -->
<mat-button></mat-button>
Production build
To create a production build that is minified, tree shaken and combined by Webpack:
The created bundle contains a set of files that can be served by a (static) webserver. Note the cache-busting.
Filename |
---|
index.html |
main.4241826d33942486d560.js |
polyfills.2f4a59095805af02bd79.js |
runtime.a66f828dca56eeb90e02.js |
styles.801710192fa9f58b78b4.css |
Folder structure
- [dist] – release bundle
- [e2e] – end-to-end tests (protractor)
-
[src]
-
[app]
- app.component.* – application component
- [models] – view models
- [page] – page component
- [services]
- [assets] – static files (images, data)
- [environments] – build environment configurations (production, development)
- index.html – HTML entry point
- main.ts – entry point
- style.css – global styles
- ...
-
[app]
- angular.json – Angular-CLI configuration file
- package.json – NPM configuration file
- ...
Demo time!
Do It Yourself
Tour of Heroes: angular.io/tutorial
Where to go next?
Read up
- Forms (three tastes)
- Modules
- Reactive JavaScript
- Routing
- Template syntax
- TypeScript
- ...and many more!
Links
- angular.io
- Home of Angular
- cli.angular.io
- Angular command line tools
- angular.io/tutorial
- Tour of Heroes tutorial
- material.angular.io
- Material Design components for Angular
- code.visualstudio.com
- Visual Studio Code
- nodejs.org
- Node.js - needed for NPM
- joriszwart.nl/talks/angular6-cli
- This presentation with step-by-step code