Angular Flashcards
What makes a “single page application” (SPA) different from a normal web page?
A single-page app reloads only the data necessary for the user. SPAs fix the problem of MPA’s where you have to reload the entire page and request a new HTML for even the smallest changes in content. The user loads the page once and then all requested data is transmitted via JSON files
SPA struggles in storing Browser history.
Explain the difference between server-side and client-side rendering
Client side rendering manages the routing dynamically without refreshing the page every time a user requests a different route. It will render all HTML, CSS, and JS files, so the screen will take longer to load.
Server-side rendering is able to display a fully populated page on the first load of any page, whereas client side displays a blank page first. Server-side loads HTML and CSS files while JS boots up in the background
What are some features of the Angular Framework?
- Cross Platform
- Built in structural elements - Injectors, Components, Pipes, Services
- High Speed and Performance
How does TypeScript relate to JavaScript? What are the major benefits of using it over JavaScript?
TypeScript is an open source superset of JavaScript (JavaScript +) that compiles to JavaScript. TypeScript offers type annotations which provide optional, static type checking at compile time. JavaScript can be used as valid TypeScript. Considered an improved JS
List the data types of TypeScript
Boolean Number String Void Null Undefined
How would you create a new Angular app?
ng new (project-name)
What is a component? How would you create one? List some other commands using the Angular CLI
Components are the most basic building block of an Angular app. They are made up of an HTML Template, class and metadata
To create a component - ng g c
ng s -o : start server
ng g s
ng new
ng serve
What files make up a component? What is the “spec” file used for?
component. spec.ts
component. ts
component. html
component. css
The spec file is used for tests in your source files
Explain the relevance of npm to Angular projects. Which file does npm use to track dependencies?
Angular applications depend on npm packages for many of its features and functions. The framework, Angular CLI and the components are all packaged as npm packages and distributed using the npm registry.
npm uses json files to track dependencies
List some decorators for Angular apps?
Decorators are a function that adds metadata to a class. They use the @ prefix
Class Decorators: @Component - Defines a Component @NgModule - Defines a Module @Pipe - Defines a pipe @Injectable
What is a directive and what are the different types? How do you tell these directives apart with syntax?
Directives are custom HTML attributes which tell angular to change the style or behavior of the DOM elements
Directive Types:
Components
Attribute Directives
Structural Directives
Attribute Directives:
NgClass - adds and removes CSS classes
NgStyle - adds an dremoves HTML styles
NgModel - adds two way data binding to HTML elemnt
Structural Directives:
ngIf - Used for a condition
ngSwitch - used as a switch statement
ngFor - used as a for loop
What is the benefit of using a directive like NgClass over the class attribute, or even property binding to the class attribute?
The ngClass directive allows you to dynamically set CSS classes on an HTML element by data binding an expression that represents all classes to be added.
ngClass is more powerful. It allows you to bind a string of classes, an array of strings, or an object.
What is a pipe? What is a service?
Pipes allow you to transform data before seeing them in the view
Pipes are simple functions to use in template expressions to accept an input value and return a transformed value. Pipes are useful because you can use them throughout your application, while only declaring each pipe once
A service is a stateless object and provides some very useful functions. it is a class with a specific purpose. They are used to share data among many components, implements application logic, and external interactions like a database connection
How would you create a custom pipe?
- Create a pipe class and decorate it with the decorator @Pipe — ng g p
- Supply a name property to be used as a template code name
- Register your pipe in the module under declarations
- Implement PipeTransform and write transformation logic
- Use your pipe in the HTML using a ‘|’
How does dependency injection work in Angular?
Dependency injection, or DI, is a design pattern in which a class requests dependencies from external sources rather than creating them. Angular's DI framework provides dependencies to a class upon instantiation. This is a technique in which class receives its dependencies from external sources rather than creating them itself. It's used in the constructor