AngularJS Online Test Questions part 2

7. Which of the following directive is used to initialize an angular app?

a. ng-model
b. ng-app
c. ng-controller
d. None of the above

Answer: b. ng-app

Explanation: ng-app directive initializes an AngularJS application


8. Which of the following is true about ng-init directive?

a. ng-init directive initializes an AngularJS Application data.
b. ng-init directive is used to put values to the variables to be used in the application.
c. All of the above
d. None of the above

Answer: c. All of the above

Explanation: The ng-init directive initializes an AngularJS application data. This directive is used to put values to the variables to be used in the application. The ng-init directive evaluates the given expression(s) and can add some unnecessary logic into the scope.


9. AngularJS initializes based upon which of the following scenario?

a. DOMContentLoaded event happens
b. document.readyState is set to complete
c. All of the above
d. None of the above

Answer: c. All of the above

Explanation: AngularJS initializes automatically upon DOMContentLoaded event or when the angular.js script is evaluated if at that time document.readyState is set to 'complete'. At this point AngularJS looks for the ngApp directive which designates your application root. If the ngApp directive is found then AngularJS will:

- load the module associated with the directive.
- create the application injector
- compile the DOM treating the ngApp directive as the root of the compilation.


10. Can an HTML page have multiple "ng-app" directive for bootstrapping multiple AngularJS application?

a. Yes
b. No

Answer: b. No

Explanation: No, an HTML page cannot have multiple "ng-app" directive for bootstrapping multiple AngularJS application. The ng-app directive is used to auto-bootstrap an AngularJS application. And according to AngularJS Documentation, only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.


11. With more than one ng-app in an HTML document (an HTML page), are all of them automatically initialized.

a. Yes
b. No
c. No, only one app is automatically initialized. Others have to be manually initialized.

Answer: c. No, only one app is automatically initialized. Others have to be manually initialized.

Explanation: Only one app is automatically initialized. To run multiple applications in an HTML document, one must manually bootstrap them using angular bootstrap service.


12. What are various possible prefixes such as 'ng-' using which Angular directives (for example, ng-app) can be defined?

a. 'ng-', 'data-ng-', 'ng:'
b. 'ng-'
c. 'ng-', 'data-ng-', 'ng:', 'x-ng-'
d. 'ng-', 'data-ng-','x-ng-'

Answer: c. 'ng-', 'data-ng-', 'ng:', 'x-ng-'

Explanation: AngularJS directives are extended HTML attributes with the prefixes such as 'ng-', 'data-ng-', 'ng:', 'x-ng-'