Angular

What is ngOnInit()?

ngOnInit( ) – This hook gets called once, after the ngOnChanges() hook. It initializes the component and sets the input properties of the component.

How are observables different from promises?

  • Observable is lazy whereas a Promise is eager.
  • Promise emits a single value, Observable emits multiple values over a period of time.
  • Promise is not Lazy. Observable is Lazy. An observable is not called until we subscribe to the observable.
  • Promise cannot be cancelled. Observable can be cancelled by using the unsubscribe() method. Observable provides operators like map, forEach, filter, reduce, retry, retryWhen etc.

What are directives in Angular?

A directive is a class in Angular that is declared with a @Directive decorator. Every directive has its own behavior and can be imported into various components of an application.

What are the Angular Route Guards and what to do with them?

As the name suggests, you can configure guards on routes in your application to control how a user navigates between them. These route gaurds (also called as functions) are called when router tries to activate or deactivate certain routes.

The general rule is that the guards are functions that are called in certain points of the router lifecycle. They return a boolean or an asynchronous response: Promise or Observable. In the case of CanActivate, the guard function is called when a user tries to navigate into the route. The component behind it will only be activated after the function returns true or the Observable/Promise will eventually return true. When the function hangs or returns false, the router will not display the route content.

What are the names of some tools used for testing Angular applications?

For testing Angular applications there are certain tools that you should use that will make testing much easier to set up and run.

Karma

Karma is a JavaScript command line tool that can be used to spawn a web server which loads your application’s source code and executes your tests. You can configure Karma to run against a number of browsers, which is useful for being confident that your application works on all browsers you need to support. Karma is executed on the command line and will display the results of your tests on the command line once they have run in the browser.

Karma is a NodeJS application and should be installed through NPM/YARN.

Jasmine

Jasmine is a behavior-driven development framework for JavaScript that has become the most popular choice for testing Angular applications. Jasmine provides functions to help with structuring your tests and also making assertions. As your tests grow, keeping them well structured and documented is vital, and Jasmine helps achieve this.

Jasmine comes with a number of matches that help you make a variety of assertions. To use Jasmine with Karma, we use the karma-jasmine test runner.

Angular Mocks

Angular also provides the angular-mocks module, which provides mocking for your tests. This is used to inject and mock Angular services within unit tests. In addition, it is able to extend other modules so that they are in sync. Having tests in sync keeps them much cleaner and easier to work with. One of the most useful parts of angular-mock is $httpBackend, which lets us mock XHR requests in tests and return sample data instead.

Mocha

This tool runs on NodeJS and is used with browsers for asynchronous testing. A feature-rich JS framework, Mocha test runs serially mapping uncaught exceptions to correct test cases. It has a flexible and an accurate reporting.

What are Modules in Typescript?

Modules are powerful way to share code between files. By using modules effectively in your project you can keep your file size small and dependencies clear.
Modules are executed within their own scope and not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms.

Example of creating a Module:

module module_name{

	class xyz{
		export foo(x, y){
		 	return x*y;
		 }
	}

}

What are the benefits of using Typescript?

  • One of the biggest advantages of Typescript is its code completion and intelligence.
  • It provides the benefits of optional static typing .Here Typescript provides types that can be added to variables, functions, properties etc.
  • Typescript has the ability to compile down to a version of JavaScript that runs on all browsers.
  • Typescript tries to extend JavaScript. Compiler generates JavaScript.
  • Typescript is a backward compatible version of JavaScript that compiles to pure JavaScript which makes their writing easier and faster.
  • Typescript is purely object oriented programming which means it is a programming paradigm based on the concepts of objects.
  • Most important advantage is it offers a compiler that can convert to JavaScript equivalent code. And it has a concept of namespace defined by a module.

To what kind of client generated views, a router can be bound?

Router can be bound to any type of client-generated views, such as link on the page, button click, drop down selection etc.