fig: rudimentary structure of a typical web page
An Angular App is a collection of Modules, called NgModules.
When a new app is generated in the CLI using ng new, a root module is created through the file app.module.ts, which makes the class AppModule available to the app.
AppModule tells Angular how to assemble components:
app.module.ts fileapp.module.ts has an @NgModule function. In the input JSON of this function:
“declarations”“imports”fig: rudimentary structure of an Ng app
Components are the fundamental building blocks of Angular applications. They:
Application Shell is the visual render of the app:
app.component.ts — the component behavior script, written in TypeScriptapp.component.html — the component’s HTML templateapp.component.css — the component’s private CSS stylesapp.component.ts has a decorator function that sets the root HTML and CSS file
@Component({ selector: 'app-root', // css-selector of root component templateUrl: './app.component.html', // shell html layout styleUrls: ['./app.component.css'] // global css file })
Creating a new Component in the CLI:
ng generate component “component-name”./src/app/ named “component-name”, which houses the Component’s .ts, .html and .css filesstructure of a typical Ng Component
The .ts file has a decorator function:
@Component({
selector: 'app-“component-name”',
templateUrl: './“component-name”.component.html',
styleUrls: ['./“component-name”.component.css']
})
where:
selector: css-selector of this new ComponenttemplateUrl: new Component’s html layoutstyleUrls: array of its css files <"cssSelector"></"cssSelector"> in the AppComponent’s .html file{{property-name}} is used in HTML to call the property valueNgOnInit() { }: function that runs when Component is initialized
<h2> {{property.name | uppercase}} Details </h2>
| is the pipe operator<h2> element uppercase*ngFor: Repeater Directive (similar to for loops)*ngIf: Conditional Directive (if else conditional)[class: some-css-class]=“some condition”: conditionally apply CSS(click)="save()”: event triggers function<app-component [property]="input-value"></app-component>
[component-property]="input-value":
input-value property of the parent Component to a property of the child Component