Follow this blog to learn about programming languages and technologies affecting your life. It gives you an insight into how to best use new technology and prepare for professional interviews.
Q 1. How to embed views inside
AngularJS application?
Ans 1.HTML
does not support embedding html pages within html page. Using AngularJS, we can
embed HTML pages within a HTML page using ng-include directive.
Q 2.
What are the steps for embedding views inside AngularJS application? Ans 2. Following steps should be followed –
1.Move the code for your main HTML file to a separate HTML file
e.g. NewView.html.
2.Then include it in your main HTML page with the help of ng-include
directive.
Example -
<div ng-include
src=”Path_Of_ViewHTMLFile”>
Here, ViewHTMLFile is the
NewView.html file we created in step 1.
Q 3. Why is view embedding
preferred in AngularJS?
Ans 3.View embedding is helpful in a situation where you need to use a large
number of views and your code becomes unmanageable.
For instance, say
your main HTML page needs to use 100 views then you can manage them well by
including views.
Q 4. What is AJAX?
Ans 4. AJAX is
an acronym for Asynchronous Javascript and XML.
AJAX is helpful in
following situations –
1.Update UI without
reloading the entire page.
2.Requesting data in the
background
3.Sending data in the
background
4.Invoking service in the
background
Q 5. How to use AJAX in
AngularJS application?
Ans 5.AngularJS
provides $https service to read data from the server. The server makes a 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.
Example – app.controller("countryController", function
($scope, $http) { var
url = "Country.txt";
Ans 1.AngularJS
supports modular approach. Modules are used to separate logics say services,
controllers, application etc. and keep the code clean. We define modules in
separate javascript files.
Q 2.
Why are modules required in AngularJS? Ans 2. Modules are required in AngularJS to make
code more manageable. We can move different components of javascript code like
controllers, app registration, etc into separate javascript files. Then, these
files are referenced from within the main HTML file. Example- <head> <script
src="angular.min.js"></script> <script
src="ApplicationModule.js"></script> <script
src="ControllerModule.js"></script> </head> In
the above example, “ApplicationModule.js” javascript file contains the code for
registering the app and “ControllerModule.js” javascript file contains the code
for controller. We have just referenced the files from within our main file. Click here to watch more informative AngularJS Tutorial videos!
Q 3. How to implement
validation in AngularJS?
Ans 3.We can use $dirty, $invalid and $error to validate form elements.
Q 4. What needs to be done
before implementing validation in AngularJS?
Ans 4. Remember
to use novalidate with a form declaration to disable any browser
specific validation.
Q 5. How to use $dirty for
validation in AngularJS?
Ans 5. $dirty
indicates that the value has changed.
Ans 1.Filters
can be used to format data or select a subset of items from array and
return it as a new array. Filters can be added to expressions by using the pipe
character |, followed by a filter.
Example-{{FirstName |
uppercase}}
RESULT -
The
uppercase filter above will display the value of FirstName in uppercase.
Q 2.
How to use lowercase filter in AngularJS? Ans 2. The lowercase
filter converts a text to lower case text. Add lowercase filter to an expression
using pipe character.
In the above example the button enables and disables based on checking and unchecking the checkbox as the value of the property – enableDisableButton changes which is bound to checkbox control. This property is also used by ng-disabled directive.
Q 4. How to use ng-show directive in AngularJS?
Ans 4.The ng-show directive shows or hides an HTML element. The ng-show directive shows (or hides) an HTML element based on the value of ng-show.
In the above example the button shows and hides based on checking and unchecking of the checkbox as the value of the property – showHide1 changes which is bound to checkbox control. This property is also used by ng-show directive.
Q 5. How to use ng-hide directive in AngularJS?
Ans 5.The ng-hide directive hides or shows an HTML element. The ng-hide directive hides (or shows) an HTML element based on the value of ng-hide.
In the above example the button shows and hides based on checking and unchecking of the checkbox as the value of the property – showHide2 changes which is bound to checkbox control. This property is also used by ng-hide directive.
Q 6. What is the difference between ng-show and ng-hide?
Ans 6.Both offer each other’s opposite functionality.
In the above example, whenever the button is clicked, the operation - "clickCounter = clickCounter + 1" is performed and the value of clickCounter is incremented by 1.
Q 8. How to display the iteration number when using ng-repeat directive in AngularJS?
Ans 8. Use $index to get the iteration number when using ng-repeat directive 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
whichaccepts $scope as a parameter which refers to the
application/module that controller is to control.
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.