| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- javascript
- Method
- 다나와
- matplotlib
- angular
- python
- 프로젝트
- keras
- adaptive life cycle
- tensorflow
- pandas
- ECS
- opencv
- 애자일
- Crawling
- Scrum
- analyzing
- TypeScript
- AWS
- instance
- Agile
- visualizing
- 크롤링
- data analyze
- Project
- webcrawling
- 자바스크립트
- algorithm
- DANAWA
- data
Archives
- Today
- Total
LiJell's 성장기
Arrays and Iteration 본문
반응형
Iterating through String Arrays
- We can use the *ngFor command in the HTML file to loop through arrays declared in the component.
- ng is stand for
next generation
- ng is stand for
- Component
colors:string[] = ['red', 'blue', 'green', 'purple'];
- HTML
<div *ngFor='let color of colors'>
{{color}}
</div>
- The result in your browser
- red
- blue
- green
- purple
Iterating with Indices
- Having access to an index within each iteration of the loop
<div *ngFor = 'let fruit of fruits; let i=index'>
Fruit {{ i }} is {{ fruit }}
</div>
- The result in your browser
- Fruit 0 is apple
- Fruit 1 is orange
- Fruit 2 is pear
- Fruit 3 is peach
- Note that our 'iterator' variable is named
i. This variable can be named anything, butindexkeyword cannot be changed
Iterating through Custom Types
- connect interfaces with *ngFor looping machanism
- Let's make an interface for a Car and put it in its own file called car.ts (.ts is the extension for TypeScript files)
export interface Car {
make: string;
model: string;
miles: number;
}
- Now that we have a Car interface, we can import that interface into our component
import { Car } from './car';
- Now we can create 3 entities of the Car type
subaru: Car = {make: 'Subaru', model: 'Outback', miles: 58232};
honda: Car = {make: 'Honda', model: 'Accord', miles: 39393};
bmw: Car = {make: 'BMW', model: 'X3', miles: 4400};
cars:Car[ ] = [this.subaru, this.honda, this.bmw];
- loop through all of cars in the HTML file
<div *ngFor="let car of cars">
{{car.make}} {{car.model}} with a mileage of {{car.miles}}
</div>
- output
- Subaru Outback with a mileage of 58232
- Honda Accord with a mileage of 39393
- BMW X3 with a mileage of 4400
반응형
'Front-End > Angular & TypeScript' 카테고리의 다른 글
| Angular Files (0) | 2022.06.02 |
|---|---|
| Custom Types (0) | 2022.05.26 |
| Intro Typescript (0) | 2022.05.26 |
| HTTP (0) | 2022.05.26 |
| The Stack (0) | 2022.05.26 |
Comments