Angular 6 CLI
up to speed in 60 minutes
- joriszwart.nl
joriszwart.nl
I CODE DREAMS
Agenda
What is Angular?
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
Tools prerequisites
I recommend using Visual Studio Code with Angular CLI.
1. Install tools
- Node.js >= 6.9.0
- NPM >= 3 (comes with Node.js)
2. Install angular-cli
Open up a terminal and execute this command and wait...
Create a new application
Your new application is only one
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
- template -
page.component.ts
- component class -
page.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!
C'est tout! Des questions?
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