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.
ng new [name] --routing --style=scss --dry-run
ng new taco-truck --routing --style=scss --dry-run
ng new blockbuster-reopening --routing --style=scss --dry-run
/src/app.component.scss
/src/app-routing-module.ts
--dry-run
Wireframing is the process of visually laying out a website in preparation for design or development.
ng serve --open
ng generate component taco-menu
ng g c taco-menu
ng g i shared/interfaces/menu-item
export interface MenuItem {
name: string;
description: string;
category: string;
price: number;
isFeatured: boolean;
}
import { MenuItem } from '../shared/interfaces/menu-item';
export class TacoMenuComponent implements OnInit {
menuItem: MenuItem = {
name: 'Taco',
description: 'Just a taco',
category: 'Tacos',
price: 1.5,
isFeatured: false
};
constructor() { }
ngOnInit() { }
}
Menu Item
{{menuItem.name}}
{{menuItem.description}}
menuItems: Array
I'm going to just add our menuItem multiple times.
ngOnInit() {
this.menuItems.push(this.menuItem);
this.menuItems.push(this.menuItem);
this.menuItems.push(this.menuItem);
}
{{item.name}}
{{item.description}}