Safe ads

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

Sunday, 16 April 2017

AngularJS Interview Questions Part - 4

AngularJS Interview Questions Part - 4



Q 1.     How to create table in AngularJS?
Ans 1. Table can be constructed by repeating its rows. So, ng-repeat directive can be used to create table easily.

Example –

Features(defined with the help of $scope service inside controller)

$scope.Features = [{ FeatureName: 'Height', FeatureValue: '6ft' }, { FeatureName: 'Weight', FeatureValue: '80 Kg' }, { FeatureName: 'Complexion', FeatureValue: 'Fair' }];
<table>
<tr ng-repeat="feature in Features">
                        <td>{{feature.FeatureName}}</td>
   <td>{{feature.FeatureValue}}</td>
                      </tr>
            </table>

Output –


                                                      AngularJS Interview Questions

Q 2.     How can application data be bound to HTML DOM in AngularJS?
Ans 2. We can bind application data to attributes of HTML DOM elements by using some directives available with AngularJS


Q 3.    How to use ng-disabled directive in AngularJS?
Ans 3. The ng-disabled directive binds AngularJS application data to the disabled attribute of HTML elements.

Example-
<td><input type="checkbox" ng-model="enableDisableButton">Disable Button</td>
 <td><button ng-disabled="enableDisableButton">Click Me!</button></td>

Output - 
AngularJS Interview Questions
AngularJS Interview Questions

 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.

Example-
<td><input type="checkbox" ng-model="showHide1">Show Button</td>
<td><button ng-show="showHide1">Click Me!</button></td>

Output - 
AngularJS Interview Questions
AngularJS Interview Questions

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.

Example-
<td><input type="checkbox" ng-model="showHide2">Hide Button</td>
<td><button ng-hide="showHide2">Click Me!</button></td>

Output - 

                                               
AngularJS Interview Questions
                                                             AngularJS Interview Questions

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.

Example –

<td><input type="checkbox" ng-model="showHide3">Hide Button</td>
<td><button ng-show="showHide3">Click Me!</button></td>
<td><button ng-hide="showHide3">Click Me!</button></td>

In the above example when the checkbox is checked, button having directive ng-show will show and button having directive ng-hide will hide.

When the checkbox is unchecked, button having directive ng-show will hide and button having directive ng-hide will show.

Q 7.     How to use ng-click directive in AngularJS?
Ans 7. The ng-click directive defines AngularJS code that will be executed when the element is being clicked.

Example –

<td><p>Total click: {{ clickCounter }}</p></td>
<td><button ng-click="clickCounter = clickCounter + 1">Click Me!</button></td>

Output - 

AngularJS Interview Questions
                                                   AngularJS Interview Questions
                                  AngularJS Interview Questions

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.
Example –

<table>
<tr ng-repeat="room in HotelRooms">
                    <td>{{$index + 1}}</td>
                    <td>{{room.Room}}</td>
                    <td>{{room.Hotel}}</td>
</tr>

</table>

Output –
AngularJS Interview Questions

Q 9.     How to create nested table in AngularJS?
Ans 9. We can create nested table in angularJS by using ng-repeat directive multiple times.

Example –

I have an array(defined array with the help of $scope service inside controller) which holds three string arrays –

$scope.Countries = [["india", "Delhi"], ["USA","Washington DC"], ["UK", "London"]];

The values of inner array will be displayed inside inner table -

<table>
<tr ng-repeat="country in Countries">
<td>
<table>
<tr ng-repeat="countryCapital in country">
<td>
{{countryCapital}}
</td>
</tr>
</table>
</td>
</tr>
</table>

Output –

AngularJS Interview Questions














Click here to watch more informative JQuery Tutorial videos!



Friday, 10 February 2017

JQuery Interview Questions Part - 4

JQuery Interview Questions Part - 4


JQuery Interview Questions 


Q 1.     What are Effects?
Ans 1. Effects are the visible changes in the appearance of the selected HTML elements that get triggered by some user action.

                      

For example –
•      Hiding an element on the click of a mouse
$(“button”).click(function()
{
        $(“p”).hide();
});

•      Change in the dimensions of an HTML element when you hover over an element

$(“button”).click(function()
{
        $(“p”).animate({height:’250px’, width:’250px’}, slow);
});

Q 2.     How is hide effect method useful?
Ans 2. “Hide” effect is used to hide a visible HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).hide(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).hide();
});

Q 3.     How is show effect method useful?
Ans 3. “Show” effect is used to show a hidden HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).show(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).show();
});

Q 4.     How is toggle effect method useful?
Ans 4. “toggle” effect is used to toggle between hide and show effects. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).toggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).toggle();
});

Q 5.     How is fadeIn effect method useful?
Ans 5. “fadeIn” effect is used to fade in a hidden HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).fadeIn(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeIn();
});

Q 6.     How is fadeOut effect method useful?
Ans 6. “fadeOut” effect is used to fade out a visible HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).fadeOut(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeOut();
});

Q 7.     How is fadeTo effect method useful?
Ans 7. “fadeTo” effect is used to fade an HTML element to a given opacity. The opacity value ranges from 0 to 1. It takes three parameters – speed, opacity and callback.

Syntax –
•      $(selector).fadeTo(speed, opacity, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeTo(“slow”,  0.3);
});

Q 8.     How is fadeToggle effect method useful?
Ans 8. “fadeToggle” effect toggles between fadeIn and fadeOut effects. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).fadeToggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).fadeToggle();
});

Q 9.     How is slideUp effect method useful?
Ans 9. “slideUp” effect is used to slideUp an HTML element. It takes two parameters – speed(optional)  and callback(optional).

Syntax –
•      $(selector).slideUp(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideUp();
});

Q 10.     How is slideDown effect method useful?
Ans 10. “slideDown” effect is used to slide down an HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).slideDown(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideDown();
});

Q 11.     How is slideToggle effect method useful?
Ans 11. “slideToggle” effect is used to toggle between slide up and slide down effects on an HTML element. It takes two parameters – speed(optional) and callback(optional).

Syntax –
•      $(selector).slideToggle(speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).slideToggle();
});

Q 12.     How is stop effect method useful?
Ans 12. The JQuery stop effect is used to stop an animation or effect before it is finished. The optional stopAll parameter specifies whether also the animation queue should be cleared or not. The optional goToEnd parameter specifies whether or not to complete the current animation immediately.

Syntax –
•      $(selector).stop(stopAll, gotoEnd)
Example –
$(“button”).click(function()
{
        $(“p”).stop();
});

Q 13.     How is animate effect method useful?
Ans 13. The JQuery animate effect is used to create custom animations. The “{parameters}” parameter defines the CSS properties to be animated.Other two parameters are - speed and callback

Syntax –
•      $(selector).animate({parameters}, speed, callback)
Example –
$(“button”).click(function()
{
        $(“p”).animate({height:’250px’, width:’250px’}, slow);
});

Q 14.     How is callback function useful?
Ans 14. A callback function is executed only after the current effect is finished.

Suppose, I hide an HTML element using hide effect and in its callback I show an alert stating “Element has been hidden”.
This alert will only be shown after the element is successfully hidden.
Example –
$(“button”).click(function()
{
        $(“p”).hide(“slow”, function(){
                alert(“Element has been hidden”);
});
});

Q 15.     What is chaining?
Ans 15. You can chain together multiple JQuery effects within a single statement. Thus, all the effects will execute in sequence.

Example –
$(“button”).click(function()
{
        $(“p”).slideUp().slideDown().css(“background-color”, “yellow”);
});