Angular Flashcards
Pre-requisites for running angular app
install node.js, that is what angular CLI runs with
Creating new app
ng new appname –defaults = to create app with defaults
cd folder
npm start
new app with single typescript file
ng new appname –routing –style scss -s -t
style scss - scss styles
-s = put style in component.ts file
-t = put the html in the component.ts file
–routing = if your app is going to have more than one page
@component -decorator
it is a directive that declares the class the decorator is a function that says pass these parameters @Component({ selector: 'app-root', selector is used as element in html like for the component to show up components are the visual piece of information that we see on the screen
Directives in angular
There are three kinds of directives in Angular:
Components—directives with a template.
Structural directives—change the DOM layout by adding and removing DOM elements.
Attribute directives—change the appearance or behavior of an element, component, or another directive.
Components are the most common of the three directives. You saw a component for the first time in the Getting Started tutorial.
Structural Directives change the structure of the view. Two examples are NgFor and NgIf. Learn about them in the Structural Directives guide.
Attribute directives are used as attributes of elements. The built-in NgStyle directive in the Template Syntax guide, for example, can change several element styles at the same time.
Creating a new Directive
ng generate directive highlight
import { Directive, ElementRef } from ‘@angular/core’;
@Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor(el: ElementRef) { el.nativeElement.style.backgroundColor = 'yellow'; } } You use the ElementRef in the directive's constructor to inject a reference to the host DOM element, the element to which you applied appHighlight.
ElementRef grants direct access to the host DOM element through its nativeElement property.
This first implementation sets the background color of the host element to yellow.
in appComponent.html:
<p>Highlight me!</p>
using the attribute directive
To summarize, Angular found the appHighlight attribute on the host <p> element. It created an instance of the HighlightDirective class and injected a reference to the </p><p> element into the directive’s constructor which sets the </p><p> element’s background style to yellow.</p>
Directives
Decorator that marks a class as an Angular directive. You can define your own directives to attach custom behavior to elements in the DOM. selector The CSS selector that identifies this directive in a template and triggers instantiation of the directive.
providers
Configures the injector of this directive or component with a token that maps to a provider of a dependency.
Subclass- component:
The options provide configuration metadata that determines how the directive should be processed, instantiated and used at runtime.
Directive classes, like component classes, can implement life-cycle hooks to influence their configuration and behavior.
Options
The CSS selector that identifies this directive in a template and triggers instantiation of the directive.
selector: string
npm start
it looks at package.json; script - sees that start - refers to ng serve and runs that command
Changing port that angular runs from 4200
npm start – –port 4201 ; it builds the application
http://localhost:4201/
Structure of application - src - app folder
src – app = this has our source code
app folder has 4 files:
appmodule.ts = it is the manifest; everything in our app
appcomponent.ts = it is the first component that loads inside our webpage
app-routing.module.ts = our routing module
spec.ts
to test your components
assets folder
if you have images or fonts put them here; it will automatically deploy for you
files index.html and main.ts
starting point of the app
when the app cranks up, index.html is the base page that loads , this page contains our app component
main.ts = this kicks off angular in the page
index.html = there is a script tag that runs main.ts which kicks off angular
——–
main.ts
platformBrowserDynamic().bootstrapModule(AppModule):
go ahead and load the browser platforms, which is angular component
bootstrap with AppModule- which means start running it
when angular starts; it says go look into AppModule for everything you need to load
package.json
our dependencies for the application lives and the scripts
angular is listed here as we are using it
scripts like npm start are listed
AppModule.ts
it says bootstrap - AppComponent
what features to use? in the imports we specify that
imports: [
BrowserModule,
AppRoutingModule
],
it contains everything that our applications needs to start
@ngModule decorator- it is a function; it says this class called AppModule is an ngModule
we declare the components used in it
we import the functionality to use
which component to start with- bootstrap
Module is just a manifest - nothing visual on the screen for the users. it helps to organize the app
@NgModule({ declarations: [ AppComponent, HighlightDirective ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
AppComponent.ts
selector in appComponent.ts - this is what is being used in index.html to list like
we prefix the selectors like app-name; so that it does not collide with normal html elements like <div></div>
creating a component
ng generate component heroes / ng g c heores -d
it creates heroes.component.ts and heroes.component…spect.ts files and updates the app.module.ts
-d = for dry run
we could directly paste the selector in the app.component.ts
Selector - the name of the html element we will use
template URL -is the location of html template for this component
the hero is a model that may contain the data we can use in our component
dry run
ng g c heroes -d
ngOnInit()
for start up code
Interpolation
Display models with {{model}}
<div> Hello {{hero.name}} </div> - this is read only data
one way property binding
<img></img> Binding a DOM property to a value or expression using square brackets []
using dot for nested properties and attr to bind to attributes
save - binding for the disabled property of a button
attribute binding
<div></div>
if there is no DOM property then we use attribute binding
Two-way data binding
initial value in the model property is shown in the html page, if any changes are made to the input element then the changes are made in the component model properties
Event binding
Execute when an event occurs , wrap with ()
(target)=”expression” or on-target=”expression”
No
if the button is clicked onNo() function is executed
- {{hero.name}}
{{heroes | json}}[ { "id": 10, "name": "John" }, { "id": 20, "name": "Peter" }, { "id": 30, "name": "Saint" } ] Pre - maintains the spacings ```
heroes works!
``` ```- {{hero.name}}
Angular Router
Crisis Center Heroes The RouterLink directives on the anchor tags give the router control over those elements. The navigation paths are fixed, so you can assign a string to the routerLink (a "one-time" binding). Had the navigation path been more dynamic, you could have bound to a template expression that returned an array of route link parameters (the link parameters array). The router resolves that array into a complete URL.[ngClass] binding to the classes property making this blue
Yet another is setting the model property of a custom component—a great way for parent and child components to communicate: src/app/app.component.html content_copyShow this only if "show" is true
Show this only if "show" is not true
Disable select