Vignesh Vignesh - 3 months ago 7
AngularJS Question

why we are using "var app = angular.module('myApp', []);" this? especially i need to know about square brackets in this line?

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
*var app = angular.module('myApp', []);*
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>





why we are using "var app = angular.module('myApp', []);" this? especially i need to know about square brackets in this line???

Answer

angulaJS has

angular.module('myApp', []);

Because you can add modules to your application ,you can inject like

angular.module('myApp', ['ngRoute','ngSanitize']

What is a Module? You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, etc.

Why? Most applications have a main method that instantiates and wires together the different parts of the application.

Angular apps don't have a main method. Instead modules declaratively specify how an application should be bootstrapped. There are several advantages to this approach:

The declarative process is easier to understand. You can package code as reusable modules. The modules can be loaded in any order (or even in parallel) because modules delay execution. Unit tests only have to load relevant modules, which keeps them fast. End-to-end tests can use modules to override configuration.

https://docs.angularjs.org/guide/module