Interview :: Angular JS
AngularJS is an open-source JavaScript framework used to build rich and extensible web applications. It is developed by Google and follows the MVC (Model View Controller) pattern. It supports HTML as the template language and enables the developers to create extended HTML tags which will help to represent the application's content more clearly. It is easy to update and receive information from an HTML document. It also helps in writing a proper maintainable architecture which can be tested at a client-side.
Some of the main advantages of AngularJS are given below:
- Allows us to create a single page application.
- Follows MVC design pattern.
- Predefined form validations.
- Supports animations.
- Open-source.
- Cross-browser compliant.
- Supports two-way data binding.
- Its code is unit testable.
There are some drawbacks of AngularJS which are given below:
- JavaScript Dependent
If end-user disables JavaScript, AngularJS will not work. - Not Secured
It is a JavaScript-based framework, so it is not safe to authenticate the user through AngularJS only. - Time Consumption in Old Devices
The browsers on old computers and mobiles are not capable or take a little more time to render pages of application and websites designed using the framework. It happens because the browser performs some supplementary tasks like DOM (Document Object Model) manipulation. - Difficult to Learn
If you are new in AngularJS, then it will not be easy for you to deal with complex entities such as Quite layered, hierarchically and scopes. Debugging the scope is believed a tough task for many programmers.
AngularJS is based on MVC framework, where MVC stands for Model-View-Controller. MVCperforms the following operations:
- A model is the lowest level of the pattern responsible for maintaining data.
- A controller is responsible for a view that contains the logic to manipulate that data. It is basically a software code which is used for taking control of the interactions between the Model and View.
- A view is the HTML which is responsible for displaying the data.
For example, a $scope can be defined as a model, whereas the functions written in angular controller modifies the $scope and HTML displays the value of scope variable.
A $scope is an object that represents the application model for an Angular application.
Each AngularJS application can have only one root scope but can have multiple child scopes. For example:
Some of the key characteristics of the $scope object are given below:
- It provides observers to check for all the model changes.
- It provides the ability to propagate model changes through the application as well as outside the system to other associated components.
- Scopes can be nested in a way that they can isolate functionality and model properties.
- It provides an execution environment in which expressions are evaluated.
AngularJS is a JavaScript framework with key features like models, two-way binding, directives, routing, dependency injections, unit tests, etc. On the other hand, JQuery is a JavaScript library used for DOM manipulation with no two-way binding features.
A term IDE stands for Integrated Development Environment. There are some IDE's given below which are used for the development of AngularJS:
- Eclipse
It is one of the most popular IDE. It supports AngularJS plugins. - Visual Studio
It is an IDE from Microsoft that provides a platform to develop web apps easily and instantly. - WebStorm
It is one of the most powerful IDE for modern JavaScript development. It provides an easier way to add dependencies with angular CLI. - Aptana
It is a customized version of Eclipse. It is free to use. - Sublime Text
It is one of the most recommendable editors for HTML, CSS, and JavaScript. It is very much compatible with AngularJS code.
Some important features of AngularJS are given below:
- MVC- In AngularJS, you just have to split your application code into MVC components, i.e., Model, View, and the Controller.
- Validation- It performs client-side form validation.
- Module- It defines an application.
- Directive- It specifies behavior on the DOM element.
- Template- It renders the dynamic view.
- Scope- It joins the controller with the views.
- Expression- It binds application data to HTML.
- Data Binding- It creates a two-way data-binding between the selected element and the $ctrl.orderProp model.
- Filter- It provides the filter to format data.
- Service- It stores and shares data across the application.
- Routing- It is used to build a single page application.
- Dependency Injection- It specifies a design pattern in which components are given their dependencies instead of hard-coding them within the component.
- Testing- It is easy to test any of the AngularJS components through unit testing and end-to-end testing.
Directives are the markers on DOM element which are used to specify behavior on that DOM element. All AngularJS directives start with the word "ng". There are many in-built directives in AngularJS such as "ng-app", "ng-init", "ng-model", "ng-bind", "ng-repeat" etc.
- ng-app
The ng-app directive is the most important directive for Angular applications. It is used to locate the beginning of an Angular application for AngularJS HTML compiler. It marks the HTML elements that Angular intends to make the root element of the application. The custom attributes use spinal-cases, whereas the corresponding directives follow the camelCase. If we do not use this directive and try to process other directives, it gives an error. - ng-init
The ng-init directive is useful for initializing the data variable's inline statement of an AngularJS application. Therefore, those statements can be used in the specified blocks where we can declare them. A directive ng-init is like a local member of the ng-app directive, and it may be a single value or a group of the values. It directly supports JSON data. - ng-model
The ng-model directive binds the values of HTML elements such as input, select, textarea to the application data. It provides two-way binding behavior with the model value. Sometimes, it is also used for databinding. - ng-bind
The ng-bind directive is used to bind the model/variable's value to HTML controls of an AngularJS application. It can also be used with HTML tags attributes like: , and more but it does not support two-way binding. We can only check the output of the model values. - ng-repeat
The ng-repeat directive is used to repeat HTML statements. It works the same as for each loop in C#, Java or PHP on a specific collection item like an array.
Let's see a simple example of AngularJS directive:
Controllers are JavaScript functions which are used to provide data and logic to HTML UI. It acts as an interface between Server and HTML UI. Each controller accepts $scope as a parameter which refers to the application/module that controller is going to control. For example: