Angular 2.0questions
Angular 2.0questions
ABCDEFG
AngularJS is a framework to build large scale and high performance web application while
keeping them as easy-to-maintain. Following are the features of AngularJS framework.
Components − The earlier version of Angular had a focus of Controllers but now has
changed the focus to having components over controllers. Components help to build the
applications into many modules. This helps in better maintaining the application over a
period of time.
Services − Services are a set of code that can be shared by different components of an
application. So for example if you had a data component that picked data from a
database, you could have it as a shared service that could be used across multiple
applications.
Modules − This is used to break up the application into logical pieces of code. Each
piece of code or module is designed to perform a single task.
Service − This is used to create components which can be shared across the entire
application.
Bootstrap array − This is used to tell Angular JS which components need to be loaded
so that its functionality can be accessed in the application. Once you include the
component in the bootstrap array, you need to declare them so that they can be used
across other components in the Angular JS application.
Export array − This is used to export components, directives, and pipes which can then
be used in other modules.
Import array − Just like the export array, the import array can be used to import the
functionality from other Angular JS modules.
Class − This is like a C or Java class which consists of properties and methods.
Metadata − This is used to decorate the class and extend the functionality of the class.
Template − This is used to define the HTML view which is displayed in the application.
ngIf −
The ngif element is used to add elements to the HTML code if it evaluates to true, else it
will not add the elements to the HTML code.
Syntax
*ngIf = 'expression'
If the expression evaluates to true then the corresponding gets added, else the elements
are not added.
ngFor −
The ngFor element is used to elements based on the condition of the For loop.
Syntax
*ngFor = 'let variable of variablelist'
In the error handler function, we send the error to the console. We also throw the error
back to the main program so that the execution can continue.
Now, whenever you get an error it will be redirected to the error console of the browser.
What is routing?
Routing helps in directing users to different pages based on the option they choose on
the main page. Hence, based on the option they choose, the required Angular
Component will be rendered to the user.
What is CLI?
Command Line Interface (CLI) can be used to create our Angular JS application. It also
helps in creating a unit and end-to-end tests for the application.
What is Dependency Injection? Explain with example.
Dependency injection is the ability to add the functionality of components at runtime.
Let's take a look at an example and the steps used to implement dependency injection.
Step 1 − Create a separate class which has the injectable decorator. The injectable
decorator allows the functionality of this class to be injected and used in any Angular JS
module.
@Injectable()
export class classname {
}
Step 2 − Next in your appComponent module or the module in which you want to use
the service, you need to define it as a provider in the @Component decorator.
@Component ({
providers : [classname]
})
Following are some key points to note about the above code.
The target for the compilation is es5 and that is because most browsers can only
understand ES5 typescript.
The sourceMap option is used to generate Map files, which are useful when debugging.
Hence, during development it is good to keep this option as true.
The "build:watch": "tsc -p src/ -w" command is used to compile the typescript in the
background by looking for changes in the typescript files.
'npm:': 'node_modules/' tells the location in our project where all the npm modules are
located.
The mapping of app: 'app' tells the folder where all our applications files are loaded.
The import statement is used to import functionality from the existing modules. Thus,
the first 3 statements are used to import the NgModule, BrowserModule and
AppComponent modules into this module.
The NgModule decorator is used to later on define the imports, declarations, and
bootstrapping options.
The BrowserModule is required by default for any web based angular application.
The bootstrap option tells Angular which Component to bootstrap in the application.
The constructor is a typescript feature. The constructor is only related to class instantiation and
it’s nothing to do with Angular 2 and it is use to some initialization processing with respect to
class hierarchies for the newly created instance.
The ngOnInit event is an Angular 2 life-cycle event/ method that are called after the
first ngOnChanges. The ngOnInitmethod is use to parameters defined with @Input otherwise the
constructor is OK.
Example
@View({
template: "<div> <h1>{{userName}}</h1> <p>{{phone}}</p>"
})
class userDetail {
constructor(public userName: string, public phone: string) {}
}
<user-detail></user-detail></div>
@Components @Directive
@Component meta-data annotation is used to register the @Directive meta-data annotation is used to
components. register the directives.
Syntax :-
Example,
@RouteConfig([
{path: '/employ/:id', component: employe, name: 'emp'},
])
Router-outlet directive :- Router-outlet directive is used to render the components for specific
location of your applications. Both the template and templateUrl render the components where
you use this directive.
Syntax :-
<router-outlet></router-outlet>
Syntax :-
<router-link></router-link>
Example,
Syntax :-
@RouteConfig([
{path: '/', component: Home_Component, as: 'Home'},
{path: '/AboutMe', component: AboutMe_Component, as: 'AboutMe' }
{path: '/ContactMe', component: ContactMe_Component, as: 'ContactMe' }
])
1.Injector
2.Provider
3.Dependency
Angular 2 is providing MVVM and MVC architecture for browser Javascript code
management. It provides modules for isolating application code, routing between controllers,
dynamic template loading and extending HTML markups in terms of components and
directives.’
By using # symbol
Pipes are used in templates to convert output to user friendly and readable form within
interpolation braces i.e. {{ release | date }}. The ‘|’ is denoted as pipe.
What is Host Decorators in Angular 2
Host decorators bind properties of component with UI element values. The properties inside
component class definition decorated with @HostBinding can be accessed in template from
assigned property i.e. @HostBinding() title = ‘My Title’
➔ AOT stands for Ahead of Time, There is no different compiler used in AOT just the sequence
of process is changed.
Instead of compiling at runtime in the browser like JIT compilation.
The components and templates are compiled at built time and converted to native JavaScript
and html.
So the browser does not have compile the application so it can directly render it and save time
and system resources.
Advantages:AOT provides build time error detection so many compile type errors are detected
before it running the application.
Faster download times as the application is compiled and bundled at build time, which
eliminates the need of loading the angular compiler and its libraries leading to a lesser runtime.
Faster Rendering time as the application is already compiled and only needs to be rendered on
the browser.
Disadvantages: Angular version lower than 4 produces large JavaScript bundles after
compilation, which defeats the purpose of AOT.
It only works only with HTML and CSS, other file types need to be built previously.
It needs a clean-up step before compiling.
➔ Change detection basically is the process of detecting the changes in the internal state of our
application which might be caused due to Events, XHR and Timers and make it visible in the
user interface.
Change detection work on the principle of zone.js in Angular 2/4.
➔ Directives in Angular are used to add behavior to our existing DOM elements.
We use @Directive meta-data annotation to register directives.
Directives in angular are used to design reusable components.
Directives do not have their own views.
What Are Event Emitters in Angular 2/4?
➔ If the change in one of the child components needs to be reflected to any of its parent
component, we can emit the event by using Event Emitter API in Angular.
EventEmitter is class present in @angular/core module, which is used by components and
directives to emit custom events.
➔ In Angular 2/4, dependency injection gives us the ability to add functionality of a component
at runtime.
It also provides us a way to create a service that is a reusable piece of code, which can be used
across our application to perform a particular function.
➔ Module loaders in Angular are used to bundle different modules that contain their
dependencies along with angular components into one bundle or multiple bundles (For lazy
loading) and load them in the browser.
What is the use of systemjs? How is webpack better to use in Angular 2/4?
➔ Systemjs is a client side module bundler in angular as it loads modules (components and
other files) on demand instead of loading an entire application at startup.
This largely reduces load times while starting up the app.
The up side of Webpack over Systemjs is that it bundles and creates a single file called
bundle.js, which contains HTML, CSS and JS etc.
While the initial load time might take a few seconds once the app is cached it becomes lightning
fast and will lead to a large boost in performance.
➔ In Angular 2/4 services are reusable function which include their properties used to perform
some common functionality which can be used by different components instead of writing the
same code again and again for different components a data service can be used by multiple
components (via dependency injection).
How to enable lazy loading in Angular 2/4?
What is viewchild?
➔ @ViewChild decorator is used when a parent component needs to communicate with and
pass data to the child component.
We need to pass the class name of the child component to @Viewchild decorator function.
➔ In Angular 2/4, services are used to communicate data between different components.
As one component could pass, the data to the service while another component could read from
it.
Therefore, this data service could pass the data from one components to multiple components.
List out the differences between ActivatedRoute and RouterState, with reference
to Angular 2.
Here are some of the differences between ActivatedRoute and RouterState with reference to
Angular 2: –
Difference
Animation in Angular 4 is available as a separate package and needs to be
imported from @angular/animations. In Angular2, it was available with
@angular/core. It is still kept the same for its backward compatibility
aspect.
Template
Angular 4 uses <ng-template> as the tag instead of <template>; the
latter was used in Angular2. The reason Angular 4
changed <template> to <ng-template> is because of the name conflict
of the <template> tag with the html <template> standard tag. It will
deprecate completely going ahead. This is one of the major changes in
Angular 4.
TypeScript 2.2
Angular 4 is updated to a recent version of TypeScript, which is 2.2. This
helps improve the speed and gives better type checking in the project.