*
Previous Front end Technologies AngularJS-QA-2 Next

AngularJS Question Answers - 1

Question: What is the role of services in AngularJS
Answer: – AngularJS Services are objects that provide separation of concerns to an AngularJS app.
– AngularJS Services can be created using a factory method or a service method.
– Services are singleton components. All components of the application (into which the service is injected) will work with single instance of the service.
– An AngularJS service allows developing of business logic without depending on the View logic which will work with it.
Question: What is the difference between one-way binding and two-way binding?
Answer: – One way binding implies that the scope variable in the html will be set to the first value its model is bound to (i.e. assigned to)
– Two way binding implies that the scope variable will change it’s value everytime its model is assigned to a different value
Question: Explain $q service in AngularJS?
Answer: "$q" is the angular service which provides promises and deferred functionality.
Question: What is DDO Directive Definition Object in AngularJS?
Answer: "DDO" is an object used while creating a custome directive.
Question: What is $rootScope and how does it relate to $scope?
Answer: $rootScope is the parent object of all $scope Angular objects created in a web page.
Question: What is the difference between $scope and scope in AngularJS?
Answer: $scope is used while defining a controller where scope is used while creating a link function for custom directive. The common part is that they both refers to scope object in AngularJS, but the difference is that $scope uses dependency injection where scope doesn't. When the arguments are passed-in via dependency injection, their position doesn't matter. So for example, a controller defined as ($scope as first parameter)
myApp.controller('MyController', ['$scope', function($scope, $http) {
OR ($scope is second parameter)
myApp.controller('MyController', ['$scope', function($http, $scope) {
                        
In both the case, the postion of $scope doesn't matter to AngularJS. Because AngularJS uses the argument name to get something out of the dependency-injection container and later use it. But in case of link function, the position of scope does matter because it doesn't uses DI. The very first parameter has to be scope and that's what AngularJS expects.
     
app.directive("myDirective", function() {
  return {
    scope: {};
    link: function(scope, element, attrs) {
      // code.
    }
  };
});
    
However you can name it anything of your choice. In below code, foo is your scope object.
link: function(foo, bar, baz) {
  // code  here.
}
Question: Explain how $scope.$apply() works
Answer: $scope.$apply re-evaluates all the declared ng-models and applies the change to any that have been altered (i.e. assigned to a new value) Explanation: $scope.$apply() is one of the core angular functions that should never be used explicitly, it forces the angular engine to run on all the watched variables and all external variables and apply the changes on their values
Question: Explain deferred and promises in AngularJS?
Answer: ‘Promises’ are post processing logics which are executed after some operation/action is completed whereas ‘deferred’ is used to control how and when those promise logics will execute. We can think about promises as “WHAT” we want to fire after an operation is completed while deferred controls “WHEN” and “HOW” those promises will execute.
Question: What are the different ways to create a direcitve?
Answer: A directive can be created by using as an attribute, element or class name.
Question: Explain what are directives ? Mention some of the most commonly used directives in AngularJS application ?
Answer: A directive is something that introduces new syntax, they are like markers on DOM element which attaches a special behavior to it. In any AngularJS application, directives are the most important components. Some of the commonly used directives are ng-model, ng-App, ng-bind, ng-repeat , ng-show etc.
Back to Index
Previous Front end Technologies AngularJS-QA-2 Next
*
*