Learn AngularJS
angular js
- 1. What is the syntax of ForEach loop? which loop would you use to parse a JSON and why?Below is the syntax of for each loop: angular.ForEach(students,function(value,key) { //some code } To parse JSON, we can use any loop, but I would use for each loop because it will minimize my code by eliminating the need to store the length of a JSON in a variable.
- 2. Explain MVC in reference to angular?AngularJs is an MVC based framework, where Model for a controller contains data, the controller for a view contains the logic to manipulate that data, and the view is the HTML that displays the data. A $scope can be considered as a model, whereas the functions written in angular controller modifies the $scope and HTML display the value of the scope variable.
- 3. What is two-way binding?Two-way binding means that when data in the view is changed the underlying model gets updated automatically and when a model from the controller is changed the view gets updated.
- 4. Can there be two ng-app for a single angular application?No, there can't be more than one ng-apps for a single AngularJS application. The ng-app directive conveys AngularJS application that it is the root element. In your HTML document, you can have a single ng-app directive only. In case of more than one ng-app directives, the first appearance will be used.
- 5. What is $scope?$scope is a model for a controller and helps the controller in interacting with the view. (This is a super short answer to this question, but it is complete in every sense. Try not to put any additional angular terms).
- 6. Name a few inbuilt angular filters?Currency, lowercase, uppercase, number, date are few inbuilt angular filters. {{nameOfStudent|uppercase}}
- 7. What are custom filters? Write down a syntax of the same?With AngularJS we can create our own filters. This can be done by associating the filter to our module. These types of filters are called custom filters. Below is the code to count the number of elements in the string by using filter: angular.module('myCountFilterApp', []) .filter('count',function() { return(function(input) { var out=[]; out=input.split(','); return out.length; }) }); In the above example, if the string is "21, 34, 45" then output after applying filter will be 3.
- 8. What is the difference between ng-if and ng-show?Ng-if doesn’t render the portion of DOM element on which it is associated if the specified condition is not met whereas ng-show renders the DOM element but set its CSS property of display to none if the specified condition is not met.
- 9. What is the purpose of the $watch?The purpose of $watch is to keep track of the old and new value of the watched expression. Below is the code of using $watch. $scope.$watch("checkInDate", function (newValue, oldValue) { console.log("I've changed : ", newValue); });
- 10. What is the purpose of $rootScope?$rootScope helps in communication between different controllers of an application. AngularJS can have only one rootScope for an app.
- 11. Write down the syntax for sending HTTP request?$http({ method: "POST", url: "URL", data: JSON.stringify(value), type: 'POST', contentType: 'application/json; charset=utf-8' }).then(function (response) { // success action });
- 12. Where should one use form action instead of $http for accessing a method on a server?Form action should be used at a place where the server-side method takes the control to some other view in other word leads to redirection whereas HTTP request should be used where the server method returns some data.
- 13. What is the purpose of find index in AngularJS and what does it return if no value is found?Find index returns the position of an element in an object. If the requested element is not found then -1 is returned. var index = $scope.items.findIndex(record => record.date =='2018-12-12'); In the above code, index of the object is returned where item.date=2018-12-12.
- 14. What is ng-init used for?Ng-init is used in a scenario where we want some action to be done before the initialization of a portion of the DOM element.
- 15. Can I set an angular variable from PHP session variable without sending an HTTP request?Yes, we can do that by injecting PHP in the required place. $scope.name=''; This will work only if you are using PHP to render the HTML and the above javascript is writter in tag inside the php file.
- 16. What is the significance of pipe operator in angularJs and What would be the result of following expression{{ Somevalue|lowercase|uppercase}} Pipe operator in AngularJS represents filters that are used on the expression. The preference order is from left to right. So, the result of the above expression would be SOMEVALUE.
- 17. Explain the following code:{{hotel.name}}Here, setFinalFilter is a custom filter used on the hotels object. The result would display the name of filtered hotels in ascending order of their minPrice.
- 18. What is service in AngularJS used for?Services in AngularJS are objects which are used to communicate within entire applications. app.service('sharedData', function () { //methods to get and set variable });
- 19. What is dependency injection and what are the benefits of it?Dependency injection is a powerful design pattern that allows separating the concerns of different components in an application and provides a way to inject the dependent component into the client component. Consider the below code: myApp.controller('myController', function ($scope, $http, $location) { //logic }); Here, a controller is declared with its dependencies. .$http, $location are all services which are injected into the controller as a dependent entity. All of them have some independent specific task associated with it. MyController does not need to create their instance, but it can directly use them.
- 20. Write down the syntax of creating a new date object?The syntax for creating new data object: $scope.newDate=new Date();
4iv>