I've seen that on AngularJS tutorials, that some people declare their controller functions like so:
function FirstController($scrope) {
// do something with $scope
}
and other have done it like this:
var FirstController = function ($scope) {
// do something with scope
}
Which way is the best way to declare a controller in your JS file, that will work best with the latest version of AngularJS ( right now 1.0.7 ), as in what are the best practices? Or does it not really matter?
In AngularJS, a controller is defined by a Javascript construction function, which is used in AngularJS scope and also the function $scope) is defined when the controller is defining and it returns the concatenation of the $scope. firstname and $scope. lastname.
AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.
AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using ng-controller directive. A controller is a JavaScript object that contains attributes/properties, and functions.
An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application. This approach will make your code cleaner and easy to maintain and upgrade. Angular creates one $scope object for each controller.
myApp.controller('myControl',['$scope',function($scope){
$scope.controlname = "Something";
}]);
The recommended way of declaring Controllers is using the array notation:
someModule.controller('MyController', ['$scope', 'dep1', 'dep2', function($scope, dep1, dep2) {
...
$scope.aMethod = function() {
...
}
...
}]);
according to angularJS site : https://docs.angularjs.org/guide/di
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With