AngularJS Interview Questions Part - 3
Q 1. What are expressions
in AngularJS?
Ans 1. AngularJS
binds data to HTML using Expressions. AngularJS expressions can be
written inside double braces:
{{expression}} or inside a directive : ng-bind=“expression”
Example – {{6 + 10}} or {{EmployeeName + “ “ + Company}}
Q 2. What can expressions
contain?
Ans 2. AngularJS expressions can contain literals, operators, and variables.
Q 3. What are controllers
in AngularJS?
Ans 3. A
controller is a JavaScript object containing attributes/properties and
functions. A Controller is defined by a JavaScript constructor function
which accepts $scope as a parameter which refers to the
application/module that controller is to control.
Q 4. How is a controller
attached to DOM?
Ans 4. A
controller is attached to DOM via the ng-controller directive.
Q 5. What does AngularJS do
when it finds ng-controller attached to DOM?
Ans 5. AngularJS
will instantiate a new Controller object, using the specified
Controller's constructor function.
Q 6. What will AngularJS
inject as parameter inside the controller’s constructor function?
Ans 6. A
new child scope will be created and made available as an
injectable parameter to the Controller's constructor function as $scope.
Q 7. Give a controller
example?
Ans 7. Below is the controller example –
<head>
<script>
var
app = angular.module("ngApp", []);
app.controller("ngController", function ($scope) {
$scope.FirstName = "Barack";
$scope.LastName = "Obama";
});
</script>
</head>
<body ng-app="ngApp"
ng-controller="ngController">
<input
type="text" ng-model="FirstName" placeholder="First
Name"/>
<input type="text"
ng-model="LastName" placeholder="Last Name"/>
</body>
In the above
example a controller named – “ngController” is defined using controller
function of the application module instance
- “app” having name – “ngApp”. As can be seen above inside the
constructor function of the controller, “$scope” parameter is injected by
AngularJS which is a service used for defining the model for application.
Using “$scope” we
define two variables “FirstName” and “LastName” having values “Barack” and
“Obama” respectively.
We attach the
“ngController” controller with body of DOM using ng-Controller directive, which
means the variables “FirstName” and “LastName” will be available inside the
body of DOM.
Q 8. How can we initialize
variables in AngularJS?
Ans 8. Variables
can be initialized in AngularJS using ng-init directive.
<body ng-app="ngApp" ng-init="Employees = [{FirstName:'steven', LastName:'WADE', Salary:60000}, {FirstName:'steve', LastName:'WATSON', Salary:50000}]">
In the above example, the variable “Employees” has been initialized to an array of objects having properties – “FirstName”, “LastName” and “Salary”.
No comments:
Post a Comment