Angular Interview question and answers

Understand What is AngularJS?

Actually AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. So AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.
So following point to be cleared this
  • This framework is developed on MVC (Model-View-Controller) design pattern.
  • It provides full featured SPA (Single Page Application) framework.
  • It supports Dependency Injection.
  • It supports two-way data binding.
  • It provides routing features.
  • Testing was designed right from the beginning; so you can build robust tests.
  • For DOM manipulation, jqLite is built-in; which is kind of like the Mini-Me of jQuery.
  • Separation of the client side of an Application from the Server side.
  • The AngularJS framework uses Plain Old JavaScript Objects(POJO), it doesn’t need the getter or setter functions.

So, AngularJS is an open-source JavaScript framework developed by Google. It is a structural framework for dynamic Web apps. It is easy to update and get information from your HTML document. It helps in writing a proper maintainable architecture, that can be tested at a client side code.

Let's Understand Directives in AngularJS?



AngularJS directives are only used to extend HTML and DOM elements' behavior. These are the special attributes, that start with ng- prefix, that tell AngularJS's HTML compiler ($compile) to attach a specified behavior to that DOM element.


AngularJS has a set of built-in directives like

  • ngBind,
  • ngModel
  • ngClass
  • ngApp
  • ngInit
  • ngRepeat
We can create our own directives for Angular to use them in our AngularJS Application with the controllers and services too. In this article, we’ll learn about some most important built-in directives like: ng-app, ng-init, ng-model, ng-bind and ng-repeat.

ng-app
It is the most important directive for an Angular Application, which is used to indicate starting of an Angular Application to AngularJS HTML compiler ($compile), like a “Main()” function in any compile time language like C#, Java or C++ etc. If we do not use this directive first and directly try to write other directives, it gives an error.

ng-init

ng-init directive is used to initialize an AngularJS Application data variable's inline statement, so that we can use those in the specified block where we declare them. It is like a local member of that ng-app and it can be a value or a collection of the values and as an array, it directly supports JSON data.

ng-model

ng-model directive is used to define the model/variables value to be used in AngularJS Application’s HTML controls like <input type=’text’/> and it also provides two-way binding behavior with the model value. In some cases, it’s also used for databinding.

ng-bind

ng-bind directive is also used to bind the model/variable's value to AngularJS Applications HTML controls as well as with HTML tags attributes like: <p/>, <span/> and more, but it does not support two way binding. We can just see the output of the model values.

ng-repeat

ng-repeat directive is used to repeat HTML statements. Ng-repeat works the same as for each loop in C#, Java or PHP on a specific collection item like an array.

Let's Understand what is filter in angularjs

So Filters are used for formatting data displayed to the user. They can be used in view templates, controllers or services. AngularJS comes with a collection of built-infilters, but it is easy to define your own as well.
Normally Filters are used to change modify the data and can be clubbed in expression or directives using pipe character. Following is the list of commonly used filters.
Sr.No.NameDescription
1uppercaseconverts a text to upper case text.
2lowercaseconverts a text to lower case text.
3currencyformats text in a currency format.
4filterfilter the array to a subset of it based on provided criteria.
5orderbyorders the array based on provided criteria.

Let's Understand uppercase filter

Add uppercase filter to an expression using pipe character. Here we've added uppercase filter to print student name in all capital letters.

Enter first name:<input type = "text" ng-model = "student.firstName"> Enter last name: <input type = "text" ng-model = "student.lastName"> Name in Upper Case: {{student.fullName() | uppercase}}

Let's Understand  lowercase filter

Add lowercase filter to an expression using pipe character. Here we've added lowercase filter to print student name in all lowercase letters.

Enter first name:<input type = "text" ng-model = "student.firstName"> Enter last name: <input type = "text" ng-model = "student.lastName"> Name in Lower Case: {{student.fullName() | lowercase}}

Let's Understand  currency filter

Add currency filter to an expression returning number using pipe character. Here we've added currency filter to print fees using currency format.

Enter fees: <input type = "text" ng-model = "student.fees"> fees: {{student.fees | currency}}

Let's Understand  filter filter

To display only required subjects, we've used subjectName as filter.
Enter subject: <input type = "text" ng-model = "subjectName"> Subject: <ul> <li ng-repeat = "subject in student.subjects | filter: subjectName"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>

Let's Understand  order by filter

To order subjects by marks, we've used orderBy marks.
Subject: <ul> <li ng-repeat = "subject in student.subjects | orderBy:'marks'"> {{ subject.name + ', marks:' + subject.marks }} </li> </ul>


Post a Comment

8 Comments

  1. I need a simple ajax tutorial or case study for a simple input form, where
    I want to post a username through an input form, which sends it to the
    database and replies with the results.
    Any recommendation for such tutorial is welcome, because I've only got one
    using Mootool but I'm searching for one using jQuery

    ReplyDelete
  2. ======================
    This is your Answer
    ====================
    $.ajax({
    url: "test.html",
    cache: false,
    success: function(html){
    $("#results").append(html);
    }
    });

    ReplyDelete
    Replies
    1. Use this code to send POST data and output result.

      var menuId = $("ul.nav").first().attr("id");
      var request = $.ajax({
      url: "script.php",
      type: "POST",
      data: {id : menuId},
      dataType: "html"
      });

      request.done(function(msg) {
      $("#log").html( msg );
      });

      request.fail(function(jqXHR, textStatus) {
      alert( "Request failed: " + textStatus );
      });

      Delete

  3. PkDomain offers Best quality web hosting, domain registration, website builder and reseller hosting with free domain registration in Pakistan. PK Domain offers Web Hosting in Pakistan, One of top quality Web Hosting Services Provider in Lahore Pakistan. Our services are available in all cities of Pakistan you can order us via www.pkdomain.com.pk. PK Domain is one of top best web hosting provider in Pakistan along with big cities, Karachi Lahore, Islamabad.

    ReplyDelete
  4. PkDomain are best & free web hosting & PK domain registration services provider company in Pakistan, get vps and Dedicated hosting in cheap rates. We provide Web Hosting for small businesses and individuals. Visit www.pkdomain.com.pk to get information about cheap domain hosting prices. Whether you need web hosting in Pakistan or a complete solution with development, we have it

    ReplyDelete