Best Angular Js Interview Questions Part – 3
What Is Constant?
constants are used to pass values at config phase considering the fact that value cannot be used to be passed during config phase.
mainApp.constant(“configParam”, “constant value”);
What Are The Differences Between Service And Factory Methods?
factory method is used to define a factory which can later be used to create services as and when required whereas service method is used to create a service whose purpose is to do some defined task.
What Is Factory Method?
Using factory method, we first define a factory and then assign method to it.
var mainApp = angular.module(“mainApp”, []);
mainApp.factory(‘MathService’, function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
What Is Service Method?
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);
}
});
What Is A Service?
Services are JavaScript functions and are responsible to do specific tasks only. Each service is responsible for a specific task for example, $http is used to make ajax call to get the server data. $route is used to define the routing information and so on. Inbuilt services are always prefixed with $ symbol.
What Is Use Of $routeprovider In Angular Js?
$routeProvider is the key service which set the configuration of urls, maps them with the corresponding html page or ng-template, and attaches a controller with the same.
What Is $rootscope?
Scope is a special JavaScript object which plays the role of joining controller with the views. Scope contains the model data. In controllers, model data is accessed via $scope object. $rootScope is the parent of all of the scope variables.
How To Make An Ajax Call Using Angular Js?
AngularJS provides $http 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, $http can be used to get the data from server in the following manner:
function studentController($scope, $http) {
var url = “data.txt”;
$http.get(url).success( function(response) {
$scope.students = response;
});
}
How To Validate Data In Angular Js?
AngularJS enriches form filling and validation. We can use $dirty and $invalid flags to do the validations in seamless way. Use novalidate with a form declaration to disable any browser specific validation.
Following can be used to track error.
$dirty −
states that value has been changed.
$invalid −
states that value entered is invalid.
$error −
states the exact error.
How Angular.module Works?
angular.module is used to create AngularJS modules along with its dependent modules.
Consider the following example:
var mainApp = angular.module(“mainApp”, []);
Here we’ve declared an application mainApp module using angular.module function. We’ve passed an empty array to it. This array generally contains dependent modules declared earlier.
Explain Ng-click Directive.
ng-click directive represents a AngularJS click event.
In below example, we’ve added ng-click attribute to a HTML button and added an expression to updated a model. Then we can see the variation.
Explain Ng-hide Directive.
ng-hide directive hides a given control.
In below example, we’ve added ng-hide attribute to a HTML button and pass it a model. Then we’ve attached the model to a checkbox and can see the variation.
Explain Order By Filter.
orderby filter orders the array based on provided criteria.
In below example, to order subjects by marks, we’ve used orderBy marks.
Subject:
- {{ subject.name + ‘, marks:’ + subject.marks }}
Explain Ng-disabled Directive.
ng-disabled directive disables a given control.
In below example, we’ve added ng-disabled attribute to a HTML button and pass it a model. Then we’ve attached the model to an checkbox and can see the variation.
Explain Ng-show Directive.
ng-show directive shows a given control.
In below example, we’ve added ng-show attribute to a HTML button and pass it a model. Then we’ve attached the model to a checkbox and can see the variation.
Explain Currency Filter.
Currency filter formats text in a currency format.
In below example, we’ve added currency filter to an expression returning number using pipe character. Here we’ve added currency filter to print fees using currency format.
fees: {{student.fees | currency}}
Explain Filter Filter.
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.
Subject:
- {{ subject.name + ‘, marks:’ + subject.marks }}
Explain Uppercase Filter.
Uppercase filter converts a text to upper case text.
In below example, we’ve added uppercase filter to an expression using pipe character. Here we’ve added uppercase filter to print student name in all capital letters.
Name in Upper Case: {{student.fullName() | uppercase}}
Explain Ng-repeat Directive.
ng-repeat directive repeats html elements for each item in a collection.
What Are Angular Js Expressions?
Expressions are used to bind application data to html. Expressions are written inside double braces like {{ expression}}. Expressions behave in same way as ng-bind directives. AngularJS application expressions are pure JavaScript expressions and outputs the data where they are used.
Explain Ng-init Directive.
ng-init directive initializes an AngularJS Application data. It is used to put values to the variables to be used in the application.
Explain Ng-controller Directive.
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.
Explain Ng-app Directive.
ng-app directive defines and links an AngularJS application to HTML. It also indicate the start of the application.
Explain Ng-model Directive.
ng-model directive binds the values of AngularJS application data to HTML input controls. It creates a model variable which can be used with the html page and within the container control( for example, div) having ng-app directive.
Explain Ng-bind Directive.
ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control’s data when model data is updated by controller.
Explain Angular Js Boot Process.
When the page is loaded in the browser, following things happen:
· HTML document is loaded into the browser, and evaluated by the browser. Angular JS JavaScript file is loaded; the angular global object is created. Next, JavaScript which registers controller functions is executed.
· Next Angular JS scans through the HTML to look for Angular JS apps and views. Once view is located, it connects that view to the corresponding controller function.
· Next, Angular JS executes the controller functions. It then renders the views with data from the model populated by the controller. The page gets ready.
Which Are The Core Directives Of Angular Js?
Following are the three core directives of AngularJS.
•ng-app −
This directive defines and links an AngularJS application to HTML.
•ng-model −
This directive binds the values of AngularJS application data to HTML input controls.
•ng-bind −
This directive binds the AngularJS Application data to HTML tags.