1

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) { }

http://jsfiddle.net/tnq86/15/

http://jsfiddle.net/cVsdp/4/

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.

5
  • The second approach is not valid and would not work. Commented Oct 3, 2015 at 17:42
  • see the js fiddle link then u can see second one is working. Commented Oct 3, 2015 at 17:43
  • 2
    You're right, it works. But it is bad practice. You should use the .controller() function to register a controller. Otherwise you're declaring it on window, which is explicitly not recommended. docs.angularjs.org/api/ng/service/$controller Commented Oct 3, 2015 at 17:47
  • 1
    Only works in older versions... global functions do not work in 1.3 and above. Must declare controller in module context now. It was an old convention they got rid of Commented Oct 3, 2015 at 17:57
  • @ZenDD not true...it works fine in 1.2 and below jsfiddle.net/cVsdp/310 Commented Oct 3, 2015 at 18:00

3 Answers 3

1

Putting aside the logic in your second controller.

The first options is the recommended one.

The only reason the second option worked in the first place was for demo's purpose and should not be used. Even more that way of declaring a controller was deprecated in 1.3+.

For more information, have a look at: https://docs.angularjs.org/guide/migration. Specifically the section that talks about Controllers.

Extract from it:

$controller will no longer look for controllers on window. The old behavior of looking on window for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to follow

1st Approach

angular
.module('App', [])
      .controller('AppCtrl', function ($scope) 
      {
       //you code
      };
});

2nd Approach

angular
.module('App', [])
      .controller('AppCtrl', ['$scope', function ($scope) {
        //you code
       };
}]);

3rd Approach

angular.module('app')
    .controller('SomeController', SomeController);

function SomeController($scope) { 
      //you code
}

I would like to suggest 2nd approach, it also work after minified JavaScript. or you can also follow the Angular Style Guide.

Comments

0

In my enterprise we are developping in angular, and the style that we use I guess it's clearer.

(function() {
  'use strict';

  function myController(_, $window) {
    ...
  }

  //Injections into the controller
  myController.$inject = ['lodash', '$window'];
  angular.module('myModule').controller('myController', myController);
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.