i am pretty new in angular js. so different approach in angular coding confuse me. i just found two different approach to write angular controller declaration.
first approach
angular.module('App', [])
.controller('AppCtrl', function ($scope) {
$scope.model = 0;
$scope.initSlider = function () {
};
});
2nd Approach
angular.module('MyApp', []);
function MyCtrl($scope) {
angular.element(document).ready(function () {
document.getElementById('msg').innerHTML = 'Hello';
});
}
just see the first code there controller declared like
.controller('AppCtrl', function ($scope) {
})
but in second one just a function has been declared as controller
function MyCtrl($scope) { }
just tell me two different approach has been taken to declare controller ? which one is right one and when people follow second approach where controller word is not used to declared controller. i need guide line to understand 2 different approach.
.controller()
function to register a controller. Otherwise you're declaring it onwindow
, which is explicitly not recommended. docs.angularjs.org/api/ng/service/$controller