-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAngular Basics.txt
More file actions
69 lines (47 loc) · 2.27 KB
/
Angular Basics.txt
File metadata and controls
69 lines (47 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Angular Basics
HTML Templates
----------------------
// app.component.html
<h3>
{{ username }}'s To Do List
<h6>{{ itemCount }} Items</h6>
</h3>
- Think of this page as the JSX content or HTML Template. This is the file that contains the HTML as well as javascript portions of the page
- Data bindings are represented in {{ }} and you can think of them as "Props" in React
- Data bindings are primitives in Angular and they tell the applicaiton to get the value of the username and itemCount properties and insert them into the HTML template
Components
-----------------
// app.component.ts
import { Component } from '@angular/core';
import { TodoList } from '../../models/TodoList';
import { TodoItem } from '../../models/TodoItem';
// this is an example of a decorator, which provides metadata about a class.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
// sample TodoList data
private list = new TodoList('Bob', [
new TodoItem('Go for run', true),
new TodoItem('Get flowers'),
new TodoItem('Collect tickets'),
]);
get username(): string {
return this.list.user;
}
get itemCount(): number {
return this.list.items.filter((item) => !item.complete).length;
}
}
- Angular components are responsible for managing a template and providing it with the data and logic it needs
- Components act as a bridge between the data model classes and the template so that we can create an instance of the TodoList class, populate it with some sample TodoItem objects, and, in doing so, provide the template with the username and itemCount properties it needs.
Data Models
----------------
// models/TodoItem.ts
export class TodoItem {
constructor(public task: string, public complete: boolean = false) {}
}
- The export keyword relates to JavaScript modules. When using modules, each TypeScript or JavaScript file is considered to be a self-contained unit of functionality, and the export keyword is used to identify data or types that you want to use elsewhere in the application.
- The class keyword declares a class, and the constructor keyword denotes a class constructor. Unlike C# and Java, JavaScript doesn’t use the name of the class to denote the constructor.