Angular JS Interview Question Set 9
Categories: Angular JS
What does the following code do? What are the permitted values of the restrict attribute?
app.directive('myFirstDirective', function() {
return {
restrict: 'E',
scope: {
directiveInfo: '=directiveInfo'
},
templateUrl: 'my-first-directive.html'
};
});
Differentiate between compile and link in AngularJS?
Compile is like a service used for traversing the HTML to find all the directives and return link functions.
The link does the task of combining the model with a view where any changes made to the model are reflected in the view and vice versa.
How does routing work in AngularJS?
Routing enables us to create different URLs according to different contents in our app which in turn enables the users of the application to bookmark the contents as per their requirements. The route is that URL that can be bookmarked. Routing also helps in developing SPA (Single Page Applications) i.e create a single HTML page and update that page dynamically as and when the user interacts.
Write a syntax to send sample HTTP POST request in AngualrJS?
To perform any AJAX calls, AngularJS makes use of the $http service. The syntax is as below:
$http({
method: "POST",
url: "URL",
data: JSON.stringify(value),
contentType: 'application/json'
}).then(function (successResponse)
{
// success callback action of the request
},function (errorResponse)
{
// error callback action of the request
});
What is the importance of the $location service?
$location is one of the built-in AngularJS services that helps to keep track of the application’s URL, parses it, and makes the value available to the controller. In case the $location value is changed in the controller, the same is reflected on the browser’s address bar. Changes to the URL on the address bar also result in reflection of the same on the $location service.
What is the importance of orderBy?
orderBy is a built-in filter in AngularJS that helps to re-order the array items based on defined criteria. For example, if we need to sort the items based on the ascending order of price, then we can follow the below code:
<ul>
<li ng-repeat = "item in items | orderBy:'price">
{{ item.name + ', price:' + item.price }}
</li>
</ul>
Why do we use ng-include?
The ng-include directive is used for helping us to embed HTML pages inside a single HTML page. For example:
<div ng-app = "" ng-controller = "interviewBitController">
<div ng-include = "'sample.htm'"></div>
<div ng-include = "'example.htm'"></div>
</div>
Is it possible to create nested controllers?
Yes, it is possible to create nested controllers in AngularJS.
The sample code snippet can be as shown below:
<div ng-controller="mainController">
<p>{{message}} {{name}}!</p>
<div ng-controller="childController1">
<p>Welcome to our app, {{name}}!</p>
<div ng-controller="subChildController2">
<p>{{message}} {{name}}! You are our esteemed guest {{name}}.</p>
</div>
</div>
</div>
What are AngularJS filters?
AngularJS filters are mainly used for formatting an expression while displaying it to the user. These can be used in controllers or services or views. AngularJS provides several inbuilt filters such as currency, filter, date, JSON, limitTo, etc whose purpose is to format the data without actually changing its value before merging to the expression and this is done by using the pipe character (|). AngularJS also provides support for registering and implementing custom filters and use them using the pipe symbol.