Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been told that I should be using the app.controller syntax, in order to support minification.

Rewriting the sample (tutorial) example, and I found that I couldn't get it to work:

use 'strict';

/* Minifiable solution; which doesn't work */
var app = angular.module('myApp', ['ngGrid']);

// phones.json: http://angular.github.io/angular-phonecat/step-5/app/phones/phones.json

app.controller('PhoneListCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('phones/phones.json').success(function (data) {
        $scope.phones = data;
    });

    $scope.orderProp = 'age';
}]);
/* Alternate [textbook] solution; which works */
function PhoneListCtrl($scope, $http) {

    $http.get('phones/phones.json').success(function (data) {
        $scope.phones = data;
    });

    $scope.orderProp = 'age';
}

PhoneListCtrl.$inject = ['$scope', '$http'];
<body ng-app="myApp" ng-controller="PhoneListCtrl">
    {{phones | json}}
</body> <!-- Outputs just an echo of the above line, rather than content -->

What do I need to change?

share|improve this question
As far as I know you can use the second version even for minimizing, because of the line .$inject - that tells angular.js the correct unchanged names of the two parameters. – flaschenpost May 21 at 7:38
It works for me. What is the error you got? – DanEEStar May 21 at 8:22

1 Answer

The way I did my controller layout is:

var app = angular.module('myApp', ['controllers', 'otherDependencies']);
var controllers = angular.module('controllers', []);
controllers.controller('PhoneListCtrl', ['$scope', '$http', function ($scope, $http) {
  // your code
  $http.get('phones/phones.json').success(function (data) {
    $scope.phones = data;
  });
}]);
share|improve this answer
How to you reference it in the view? – user2283066 May 21 at 13:06
{{phones}} is correct, since the controller put data in $scope.phones – Foo L May 21 at 16:25
You said the 2nd format works, so it must be how you laid out the modules. – Foo L May 21 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.