Angular (web framework): Difference between revisions
Line 97: | Line 97: | ||
* The outgoing request is not a <code>GET</code> or <code>HEAD</code> request. | * The outgoing request is not a <code>GET</code> or <code>HEAD</code> request. | ||
* The outgoing request path is a relative path of form <code>api/endpoint</code> or <code>//example.com/api/endpoint</code>. | * The outgoing request path is a relative path of form <code>api/endpoint</code> or <code>//example.com/api/endpoint</code>. | ||
You can also set the name of the cookie and name of the header to something else if you prefer. | |||
==Resources== | ==Resources== |
Revision as of 07:06, 23 August 2020
Angular is a web framework by Google which allows you to create progressive web apps (PWAs) which are single page applications (SPA).
It can also be used to create native mobile or desktop apps.
Note that this is a front-end framework only. You will need to pair it with a back-end framework to get stateful functionality.
This page is about Angular, also known as Angular 2. For AngularJS, the predecessor to Angular, see https://angularjs.org/.
Getting Started
See Angular Guide Setup Local.
- Install NodeJS and npm
- Install the Angular CLI:
npm install -g @angular/cli
Creating a project
ng new <project-name>
ng serve --open
Usage
Components
ng generate component <name>
Components are the building blocks of Angular.
Each component consists of a HTML block and an associated JS class.
Services
Services are singleton classes which can be accessed from any component.
ng generate service <name>
To access a service from a component, add a reference to the service as a parameter to the constructor of your component.
Routing
ng generate module app-routing --flat --module=app
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HeroesComponent } from './heroes/heroes.component';
const routes: Routes = [
{ path: 'heroes', component: HeroesComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Deployment
The basic idea is to do the following:
- Create a production build:
ng build --prod
- Copy the dist folder to the server.
- Redirect missing files to index.html.
Apache
Place the following in the apache config for your site or in an .htaccess
file:
RewriteEngine On # If an existing asset or directory is requested go to it as it is RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] # If the requested resource doesn't exist, use index.html RewriteRule ^ /index.html
Libraries
Some interesting libraries for Angular
Advanced Usage
CSRF/XSRF
See http#security-xsrf-protection
By default, the HttpClientModule
will automatically reflect the XSRF-TOKEN
cookie as X-XSRF-TOKEN
if the following conditions are met:
XSRF-TOKEN
is a cookie under/
withHttpOnly
set to false.- The outgoing request is not a
GET
orHEAD
request. - The outgoing request path is a relative path of form
api/endpoint
or//example.com/api/endpoint
.
You can also set the name of the cookie and name of the header to something else if you prefer.
Resources
- Official Angular Tutorial
- Teaches you to build a simple Tour of Heros app in less than 1 hour.
- Official Angular Cheat Sheet