Safe ads

Saturday 6 May 2017

AngularJS Interview Questions Part - 7

AngularJS Interview Questions Part - 7


 


Q 1.     How to embed views inside AngularJS application?
Ans 1. HTML does not support embedding html pages within html page. Using AngularJS, we can embed HTML pages within a HTML page using ng-include directive.

Q 2.     What are the steps for embedding views inside AngularJS application?
Ans 2. Following steps should be followed –
1.   Move the code for your main HTML file to a separate HTML file e.g. NewView.html.
2.   Then include it in your main HTML page with the help of ng-include directive.

Example -
<div ng-include src=”Path_Of_ViewHTMLFile”>
Here, ViewHTMLFile is the NewView.html file we created in step 1.


Q 3.    Why is view embedding preferred in AngularJS?
Ans 3. View embedding is helpful in a situation where you need to use a large number of views and your code becomes unmanageable.

For instance, say your main HTML page needs to use 100 views then you can manage them well by including views.

Q 4.     What is AJAX?
Ans 4. AJAX is an acronym for Asynchronous Javascript and XML.

AJAX is helpful in following situations –

1.   Update UI without reloading the entire page.
2.   Requesting data in the background
3.   Sending data in the background
4.   Invoking service in the background

Q 5.     How to use AJAX in AngularJS application?
Ans 5. AngularJS provides $https service to read data from the server. The server makes a call to get the desired records. AngularJS needs data in JSON format. Once the data is ready, $https can be used to get the data from server.

Example –
app.controller("countryController", function ($scope, $http) {
            var url = "Country.txt";

            $http.get(url).then(function (response) {
                $scope.countries = response.data;
            });
        });

In the above example, $http service is being injected which gets data from a URL and the data is then assigned to the model property – “countries”.




1 comment: