There are times where you may need to hide or show content based off a condition in JavaScript. In the past, we've use CSS and classes to hide elements. We can use ngIf to show or hide content.
NgIf actually adds and removes content from the DOM when its condition is triggered.
@Component({
selector: 'my-app',
template: `
<input id="textField" value="" />
<span *ngIf="isInvalid">First name is Invalid</span>
`
})
export class AppComponent {
if (txtField == ''){
isInvalid = true
}
}
Visible shown content
Hidden else content
ngFor
allows you to loop through and display an array of objects in the view.
export class AppComponent {
names = ['Ted',
'Fred',
'Marco',
'Polo'];
}
-
{{name}}
-
Ted
-
Fred
-
Marco
-
Polo
Styling components in Angular is different than traditional styling.
@Component({
selector: 'app-comp',
template: `
Hello Angular
`,
styles: [`
h1 { font-weight: normal; }
h2 { font-weight: bold; }
`]
})
export class HeroAppComponent {
/* . . . */
}
@Component({
selector: 'app-comp',
template: `
Hello Angular
`,
styleUrls: ['app.component.css']
})
export class HeroAppComponent {
/* . . . */
}
@Component({
selector: 'app-comp',
template: `
Hello Angular
`,
styles: [`
h1 { font-weight: normal; }
h2 { font-weight: bold; }
`]
})
export class HeroAppComponent {
/* . . . */
}
<h1 _ngcontent-pmm-1>Hello Angular</h1>
h1[_ngcontent-pmm-1] { font-weight: normal; }
For "common" components, don't build your own. Use a library like PrimeNG and override the styling for your project.