Just diving into AngularJS and I've discovered two different ways to declare functions:
The first way:
var myApp = angular.module('myApp', []);
myApp.controller('mainCtrl', function($scope){
$scope.message = 'Yes';
})
myApp.controller('anotherCtrl', function($scope){
$scope.message = 'No';
})
In the second method:
var myApp = angular.module('myApp', []);
myApp
.controller('mainCtrl', mainCtrl)
.controller('anotherCtrl', anotherCtrl)
function mainCtrl($scope){
$scope.message = 'Yes';
}
function anotherCtrl($scope){
$scope.message = 'No';
}
I found that with the first approach, I could organize functions in separate files like controllers.js
for Controllers, directives.js
for directives, etc...
However, when attempting the second method, errors occurred if functions were declared in different files. This makes sense since they are called in one file but defined separately. Despite this, the second method appealed to me for its readability, with less nesting involved.
So, what exactly sets these two methods apart?