This project was generated with Angular CLI version 13.2.6.
npm install -g @angular/cli@latest
ng new my-dream-app --no-strict
- ng serve
- http://localhost:4200/
- npm install --save bootstrap@3
- npm i bootstrap
- angular.json
- "styles": [ node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ],
- ng generate component servers
- check app.module.ts and declare your component
selector: 'app-root', // this is the self-made html tag templateUrl: './app.component.html', // the actual html styleUrls: ['./app.component.css'], // the actual css
selector: '[app-servers]', // html attribute selector: '.app-servers', // html class
app.component.html
- Don't have to rel stylesheet.
- app.component.css automatically adds styles to html.
- ngStyle gives behaviour when styling (css) should occur.
-
{{ "Server" }} with ID {{ serverId }} is {{ getServerStatus() }}
- // if online green color, if not red when offline getColor() { return this.serverStatus === 'online' ? 'green' : 'red'; }
- ngClass allows us to dynamically (behaviourally) add or remove CSS classes.
- ngClass gives behaviour for specific css classes when the styling should occur.
- ngClass only adds a CSS class if a certain condition is true.
- styles: [
.online { color: white; }
, ], -
- for of loop in your html
- servers = ['Testserver', 'Testserver 2'];
- <app-server *ngFor="let server of servers">
- onCreateServer() { this.serverCreated = true; this.servers.push(this.serverName); // push servername into servers array this.serverCreationStatus = 'Server was created! ' + this.serverName; }
- Root component
- Header
- Shopping list (feature)
- Shopping List
- Shopping List Edit
- Recipe book (feature)
- Recipe List
- Recipe Item
- Recipe Detail (detail page)
- Only change the element they were placed on.
- In case messy CSS dump everything in styles css in the src folder.
- app.component.ts there are inline or outline html
- selector: '[app-servers]', // html attribute
template:
<app-server></app-server> <app-server></app-server>
,
// Getter getServerStatus() { return this.serverStatus; }
- Communication between TypeScript (business logic) and HTML (template)
- Click listener HTML -> Typescript
- Event binding
- (event)="expression"
- [(ngModel)="data"]
- {{ data }}
- You can also use strings or functions that return strings.
- {{ "Server" }}
- {{ getServerStatus() }}
- To make HTML attributes work dynamically (behaviour).
- e.g. bool value
- In template (HTML) [disabled]="!allowNewServer" ! means you want to enable it (the switch to true should be in the constructor).
- allowNewServer = false;
- settimeout 2sec then true;
- [property]="data"
- You can pick property binding or string interpolation.
- DON'T MIX PROPERTY BINDING AND STRING INTERPOLATION.
- Property Binding: If you want to change an HTML attribute dynamically.
- String interpolation: When you want output text.
- Is interesting for click events
- <button class="btn btn-primary" [disabled]="!allowNewServer" (click)="onCreateServer()"
- x.component.ts needs a handler for the event e.g. onCreateServer()
- $event = event object access
- event object
- <input type="text" class="form-control" (input)="onUpdateServerName($event)" /> onUpdateServerName(event: Event) { this.serverName = (event.target).value; // what the user entered }
- Saves input and output
- [(ngModel)]="serverName"
- Input value stays saved in text field.
- Is like real-time update of the variable.
- Instructions in the DOM.
- Components are instructions in the DOM.
- in this place
-
Receives a green background!
- @Directive decorator where you create the selector and create the logic inside the class.
- To output data on basis of if/else statements
- A structural directive
- Like a lightbulb it is off then turn it down with the button then show content.
-
Server was created, server name is {{ serverName }}
- Add or not add in the DOM.
-
Server was created, server name is {{ serverName }}
-
<ng-template #noServer>
No server was created!
- Changes the structure of the DOM
- It adds this element or doesn't add it
- x.component.ts
- // Normally data is coming from HTTP request / database / calculation export class ServerComponent { girlfriend: "Karina"; }
- Use data in html template
- Large project folder -> Moving or copying folders just takes too long, make sure the installation path is correct.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI Overview and Command Reference page.