Skip to content

Commit 0e8e851

Browse files
committed
add english docs for day 4
1 parent 24116ee commit 0e8e851

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 100 Days of Code Angular Day 4
2+
![Background for day 4][bg-url]
3+
## ANGULAR STRUCTURE DIRECTIVE
4+
In programing, we need to depend on conditions to make the corresponding decision. Assume that we're developing the online video-watching application which requires the user must be older than 13 years old to watch them. Well, How to help the users know how to they ar egliable to watch them? In case that, we can now use the IF_ELSE that Angular provides to meet that requirements.
5+
6+
With Angular application, if you want to update or change the structure (etc structure HTML) in view's component, we'll use **Structure Directive**.
7+
8+
### IF-ELSE STRUCTURE
9+
To display partial's view with a condition, we'll put on the tag with a property, which contains * (_asterisk_) such as `*ngIf="expression"`:
10+
11+
```ts
12+
@Component({
13+
selector: 'app-hello',
14+
template: `
15+
<h2>Hello there!</h2>
16+
<h3>Your name: {{ user.name }}</h3>
17+
<p>Your name: {{ user.age }}</p>
18+
<div *ngIf="user.age >= 13">
19+
You are egliable to watch the content of PG-13
20+
</div>
21+
`
22+
})
23+
24+
export class HelloComponent {
25+
user= {
26+
name: 'Tam Huynh',
27+
age: 22
28+
};
29+
}
30+
```
31+
Ok, we now can display view depending on the data with the value that the expression returns. Displays if **Truthy**, **Falsy** doesn't.
32+
33+
With the Angular's built-in directive, the component's httml template is very flexible.
34+
35+
So what if we want to use **IF-ELSE**. How we do it! Maybe you'll think about the negative statement of IF statement? Right, it's completely.
36+
37+
```angular2html
38+
<div *ngIf="user.age >= 13">
39+
You are egliable to watch the content of PG-13
40+
</div>
41+
<div *ngIf="user.age < 13">
42+
You are NOT egliable to watch the content of PG-13
43+
</div>
44+
```
45+
In others hand, we have another good way, which is to use **ng-template**. **ng-template** tag is a tag provided by Angular, it contains the template defined within it's opening/closing tag. What's defined template within it won't be shown to view but we can use the template to render by code. The above block code can be converted equivalent:
46+
47+
```angular2html
48+
<div *ngIf="user.age >= 13; else noPG13">
49+
You are egliable to watch the content of PG-13
50+
</div>
51+
<ng-template #noPG13>
52+
<div>
53+
You are NOT egliable to watch the content of PG-13
54+
</div>
55+
</ng-template>
56+
```
57+
### NG-TEMPLATE
58+
With the syntax that uses with * (_asterisk_), you feel probably confused but in fact, it is called **Syntactic sugar** ( help you be easier to read more and more easily). You can convert it to property binding such as:
59+
60+
```angular2html
61+
<ng-template [ngIf]="user.age >= 13" [ngIfElse]="noPG13">
62+
<div>
63+
You are egliable to watch the content of PG-13
64+
</div>
65+
</ng-template>
66+
```
67+
## SUMMARY
68+
In the day 4, we need to understand how to use the structure of ngIf-else. In addition to the above ways, Angular also provides the way to use **ngIf - then - else**, you can learn more at the below links.
69+
70+
Link document for day 4
71+
- https://angular.io/guide/structural-directives
72+
- https://angular.io/api/common/NgIf
73+
74+
### The content of day 5 will be ngForOf loop.
75+
76+
## Contributing
77+
78+
1. Fork it (<https://github.com/tamht298/100DaysOfCodeAngular/fork>)
79+
2. Create your feature branch (`git checkout -b feature/fooBar`)
80+
3. Commit your changes (`git commit -m 'Add some fooBar'`)
81+
4. Push to the branch (`git push origin feature/fooBar`)
82+
5. Create a new Pull Request
83+
84+
HASHTAG
85+
86+
***`#100DaysOfCodeAngular`*** ***`#100DaysOfCode`*** ***`#AngularVietNam100DoC_Day4`***
87+
88+
Link reference post: [here][post-url]
89+
90+
@Copyright by [Tiep Phan](https://www.facebook.com/pttiep)
91+
<!-- Markdown link & img dfn's -->
92+
[post-url]: https://www.facebook.com/groups/AngularVietnam/permalink/892213474610838/
93+
[bg-url]: https://github.com/tamht298/100DaysOfCodeAngular/blob/d-4/day-4/day-04.png
94+

0 commit comments

Comments
 (0)