AngularJS Interview Questions Part - 6
Q 1. What are modules in
AngularJS?
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.
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.
Example-
<form
name="companyForm" novalidate>
<input
name="companyname" type="text" ng-model="companyName">
<span
style="color:red" ng-show="companyForm.location.$dirty">
<span>Location has been
changed.</span>
</span>
</form>
RESULT -
In the
above example, the error “Location has been changed.” in red would only be
shown when $dirty is true.
Q 6. How to use $invalid
for validation in AngularJS?
Ans 6. $invalid
indicates that value entered is invalid.
Example –
<form
name="companyForm" novalidate>
<input
name="CEOEmail" type="email" ng-model="email"
length="100">
<span
style="color:red" ng-show="companyForm.CEOEmail.$invalid">
<span>Email
Id is invalid.</span>
</span>
</form>
In the
above example, the error “Email Id is invalid.” in red would only be shown when
$invalid is true.
RESULT –
Q 7. How to use $error for
validation in AngularJS?
Ans 7. $invalid
indicates the exact error.
Example –
<form
name="companyForm" novalidate>
<input
name="companyname" type="text" ng-model="companyName"
required>
<span
style="color:red"
ng-show="companyForm.companyname.$error.required">
<span>Company
Name is required.</span>
</span>
</form>
In the
above example, the error “Company Name is required.” in red would only be shown
when $error.required is true.
RESULT -
No comments:
Post a Comment