21⟩ Tell me what are the key features of AngularJS?
The key features of AngularJS are
☛ Scope
☛ Controller
☛ Model
☛ View
☛ Services
☛ Data Binding
☛ Directives
☛ Filters
☛ Testable
“AngularJS Developer related Frequently Asked Questions by expert members with job experience as AngularJS Developer. These questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts”
The key features of AngularJS are
☛ Scope
☛ Controller
☛ Model
☛ View
☛ Services
☛ Data Binding
☛ Directives
☛ Filters
☛ Testable
Compilation of HTML process occurs in following ways
☛ Using the standard browser API, first the HTML is parsed into DOM
☛ By using the call to the $compile () method, compilation of the DOM is performed. The method traverses the DOM and matches the directives.
☛ Link the template with scope by calling the linking function returned from the previous step
Each angular application consist of one root scope but may have several child scopes. As child controllers and some directives create new child scopes, application can have multiple scopes. When new scopes are formed or created they are added as a children of their parent scope. Similar to DOM, they also creates a hierarchical structure.
This question might seem basic at first glance, but what you’re really doing is giving the developer a chance to show you what they know about your chosen framework. AngularJS is a powerful JavaScript-based development framework designed to create dynamic single-page applications with fewer lines of code. Some of the key advantages that you’ll want to look for in their response are listed below.
☛ Data binding is as easy as writing in your code.
☛ AngularJS was made for CRUD applications, which happen to represent the majority of web apps (excluding DOM manipulation-intensive applications like games and GUI editors).
☛ It separates DOM manipulation from app logic, making code modular and easy to test.
☛ It’s a comprehensive client-side solution in that it decouples the client-side from server-side development effort.
☛ It saves months of development time by freeing the developer from having to write repetitive low-level DOM manipulation tasks, manually registering callbacks, and otherwise automating most AJAX application tasks.
☛ It’s great for providing a “desktop-like” experience to the end user.
It is definitely a bad practice. We need to stay away from jQuery and try to realize the solution with an AngularJS approach. jQuery takes a traditional imperative approach to manipulating the DOM, and in an imperative approach, it is up to the programmer to express the individual steps leading up to the desired outcome.
AngularJS, however, takes a declarative approach to DOM manipulation. Here, instead of worrying about all of the step by step details regarding how to do the desired outcome, we are just declaring what we want and AngularJS worries about the rest, taking care of everything for us.
The two officially recommended methods for production are disabling debug data and enabling strict DI mode.
The first one can be enabled through the $compileProvider:
myApp.config(function ($compileProvider) {
$compileProvider.debugInfoEnabled(false);
});
That tweak disables appending scope to elements, making scopes inaccessible from the console. The second one can be set as a directive:
<html ng-app=“myApp” ng-strict-di>
The performance gain lies in the fact that the injected modules are annotated explicitly, hence they don’t need to be discovered dynamically.
You don’t need to annotate yourself, just use some automated build tool and library for that.
Two other popular ways are:
Using one-time binding where possible. Those bindings are set, e.g. in “{{ ::someModel }}” interpolations by prefixing the model with two colons. In such a case, no watch is set and the model is ignored during digest.
Making $httpProvider use applyAsync:
myApp.config(function ($httpProvider) {
$httpProvider.useApplyAsync(true);
});
… which executes nearby digest calls just once, using a zero timeout.
Using a service is definitely easy to test. Services are injected, and in a test either a real service can be used or it can be mocked.
Events can be tested. In unit testing controllers, they usually are instantiated. For testing events on $rootScope, it must be injected into the test.
Testing $rootScope against the existence of some arbitrary models is testable, but sharing data through $rootScope is not considered a good practice.
For testing direct communication between controllers, the expected results should probably be mocked. Otherwise, controllers would need to be manually instantiated to have the right context.
Using service method, we define a service and then assign method to it. We've also injected an already available service to it.
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
filter filter is used to filter the array to a subset of it based on provided criteria.
In below example, to display only required subjects, we've used subjectName as filter.
Enter subject: <input type = "text" ng-model = "subjectName">
Subject:
<ul>
<li ng-repeat = "subject in student.subjects | filter: subjectName">
{{ subject.name + ', marks:' + subject.marks }}
</li>
</ul>
In this section, the development environment setup has been described. Angularjs 2.0 application can be developed using:
angular-cli: This is a command line tool for Angularjs 2.0 application development which support live coding and deployment instantly. This is much easier and much straight forward.
systemjs: This is the traditional way of developing angularjs application. This way has been described in details later in Part-2.
☛ ng-app : Initializes application.
☛ ng-model : Binds HTML controls to application data.
☛ ng-Controller : Attaches a controller class to view.
☛ ng-repeat : Bind repeated data HTML elements. Its like a for loop.
☛ ng-if : Bind HTML elements with condition.
☛ ng-show : Used to show the HTML elements.
☛ ng-hide : Used to hide the HTML elements.
☛ ng-class : Used to assign CSS class.
☛ ng-src : Used to pass the URL image etc.
AngularJS combines the functionalities of most of the 3rd party libraries, it supports individual functionalities required to develop HTML5 Apps. While Backbone.js do their jobs individually.
AngularJS supports inbuilt internationalization for three types of filters currency, date and numbers. We only need to incorporate corresponding js according to locale of the country. By default it handles the locale of the browser. For example, to use Danish locale, use following script
<script src = "https://code.angularjs.org/1.2.5/i18n/angular-locale_da-dk.js"></script>
A directive is something that introduces new syntax, they are like markers on DOM element which attaches a special behavior to it. In any AngularJS application, directives are the most important components.
Some of the commonly used directives are ng-model, ng-App, ng-bind, ng-repeat , ng-show etc.
– One way binding implies that the scope variable in the html will be set to the first value its model is bound to (i.e. assigned to)
– Two way binding implies that the scope variable will change it’s value everytime its model is assigned to a different value
Data binding is the automatic synchronization of data between model and view components. ng-model directive is used in data binding.
ng-controller directive tells AngularJS what controller to use with this view. AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is a JavaScript object containing attributes/properties and functions. Each controller accepts $scope as a parameter which refers to the application/module that controller is to control.
AngularJS provides $https: control which works as a service to make ajax call to read data from the server. The server makes a database call to get the desired records. AngularJS needs data in JSON format. Once the data is ready, $https: can be used to get the data from server in the following manner:
function studentController($scope,$https:) {
var url = "data.txt";
$https:.get(url).success( function(response) {
$scope.students = response;
});
}
Factory method is used for creating a directive. It is invoked when the compiler matches the directive for the first time. We can invoke the factory method using $injector.invoke.
Syntax: module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.
One of the filters in AngularJS is the Currency Filter. This “currency” filter includes the “$” Dollar Symbol as the default. So we can use the following code as the html template format of Currency Filter.
{{ currency_expression | currency : symbol : fractionSize}}
How to use Currency Filter in AngularJS
There are two ways by which we can use Currency Filter.
Default
If we did not provide any currency symbol then by default Dollar-Sign will be used; we can use it as follows:
<!-- by default -->
Default Currency {{amount | currency}}
User Defined
To use different type of currency symbols we have to define our own symbol by using the unicode or Hexa-Decimal code of that Currency.
E.g. - For Example If we want to define Indian Currency Symbol then we have to use (Unicode-value) or (Hexa-Decimal value)
Indian Currency {{amount | currency:"&# 8377"}}