Safe ads

Showing posts with label Part 3. Show all posts
Showing posts with label Part 3. Show all posts

Sunday, 2 April 2017

AngularJS Interview Questions Part - 3


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”.

Click here to watch informative JQuery Tutorial videos!

Friday, 3 February 2017

JQuery Interview Questions part - 3

JQuery Interview Questions Part - 3


JQuery Interview Questions 


Q 1.     What are Events?
Ans 1. User’s actions to which a website can respond to are called events.

For example –
•      Clicking an element
$(“button”).click(function()
{
        Your action here on button click…
});

•      Moving mouse over an element
$(“div”).mouseover(function()
{
        Your action here when mouse moves over div…
});

               

Q 2.     What are the various JQuery event methods available?
Ans 2. Following are the various JQuery event methods available –

       Mouse Events:
1.   click
2.   dblclick
3.   mouseenter
4.   mouseleave

        Keyboard Events:
1.   keypress
2.   keydown
3.   keyup

Form Events:
1.   submit
2.   change
3.   focus
4.   blur

Document/Window Events:
1.   load
2.   resize
3.   scroll
4.   unload

Q 3.     How to use JQuery ready method?
Ans 3. The JQuery ready method is used to perform action after the HTML document has loaded.

The function within the “ready” event method executes only after the document has loaded.

SYNTAX -
$(document).ready(function() {
Your code here will execute only after the HTML document has loaded…
});

Example –
$(document).ready(function() {
        $(“button”).click(function(){
                $(“p”).hide();
});
});

The action of hiding the <p> element on button click will execute only after the entire HTML document has loaded.

Q 4.     How to use JQuery click method?
Ans 4. The JQuery click method is used to perform an action after a selected HTML element is clicked i.e. the function within the “click” event method executes only after the user clicks the selected HTML element

SYNTAX -
$(selector).click(function()
{
Your action here on element click…
});

Example –
$(document).ready(function() {
        $(“button”).click(function(){
                $(“p”).hide();
});
});

When user clicks the button the “<p>” element gets hidden.

Q 5.     How to use JQuery mouseenter method?  
Ans 5. The JQuery mouseenter method is used to perform an action when the mouse pointer enters the selected HTML element i.e. the function within the “mouseenter” event method executes only after the mouse pointer enters the selected HTML element.

SYNTAX –
$(selector).mouseenter(function()
{
Your action here when mouse pointer enters the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).mouseenter(function(){
                alert(“Mouse pointer has entered div element”);
});
});

An alert will be shown when the mouse pointer enters the “<div>” element

Q 6.    How to use JQuery mouseleave method?
Ans 6. The JQuery mouseleave method is used to perform an action when the mouse pointer leaves the selected HTML element i.e. the function within the “mouseleave” event method executes only after the mouse pointer leaves the selected HTML element.

SYNTAX –
$(selector).mouseleave(function()
{
Your action here when mouse pointer leaves the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).mouseleave(function(){
                alert(“Mouse pointer has left div element”);
});
});

An alert will be shown when the mouse pointer leaves the “<div>” element

Q 7.     How to use JQuery hover method?
Ans 7. The JQuery hover method gives a combined effect of both the mouseenter and mouseleave methods. It is used to perform an action when the mouse pointer enters a selected HTML element and then an action when it leaves that element.

SYNTAX –
$(selector).hover(function()
{
Your action here when mouse pointer enters the selected HTML element…
},
function()
{
Your action here when mouse pointer leaves the selected HTML element…
});

Example –
$(document).ready(function() {
        $(“div”).hover(function(){
                alert(“Mouse pointer has entered div element”);
},
function(){
        alert(“Mouse pointer has left div element”);
});
});

The first function is dedicated for mouseenter event and the second for mouseleave event.

Q 8.    How to use JQuery on method?
Ans 8. The “on” event method attaches one or more event handlers to the selected HTML element.

SYNTAX: Attaching one event handler–
$(selector).on(“Event_Name”, function(){action here…});

SYNTAX: Attaching multiple event handlers–
$(selector).on(
        {
  Event_Name1:function()
                {
                        Your action here for Event_Name1…
                },
          Event_Name2:function()
                {
                        Your action here for Event_Name2…
                },
          .
          .
          .
          Event_NameN:function()
                {
                        Your action here for Event_NameN…
                }
        }
);

Example: Attaching one event handler–
 $(document).ready(function() {
        $(“div”).on(“click”, function(){
                alert(“Div has been clicked”);
});
});

The above example attaches a click event handler with the “<div>” element. When the “<div>” element is clicked the above alert will be shown.

Example: Attaching multiple event handlers–
 $(document).ready(function() {
        $(“div”).on(
{
click: function()
{
                        alert(“Div has been clicked”);
},
dblclick: function()
{
                alert(“Div has been double clicked”);
}
});
});

The above example will attach two event handlers one for “click” and another for “dblclick” to “<div>”. When “<div>” element is clicked the first alert is shown and when it is double clicked, second alert is shown.

Q 9.    How to use JQuery focus method?
Ans 9. The JQuery focus method is used to perform an action after the form field is focused.

SYNTAX-
$(selector).focus(function(){
        Your action here after the form field gets focused…
});

Example –
$(document).ready( function(){
$(“input”).focus(function(){
        alert(“The input element of form has been focused”);
});
});

The above alert is shown when the input element of the form element is focused.

Q 10. How to use JQuery blur method?
Ans 10. The JQuery focus method is used to perform an action after the form field loses focus.

SYNTAX-
$(selector).blur(function(){
        Your action here after the form field loses focus…
});

Example –
$(document).ready( function(){
$(“input”).blur(function(){
                alert(“The input element of form has lost focus”);
});

});