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: .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?
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 edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked May 21, 2013 at 7:22 user2283066user2283066 1411 gold badge2 silver badges6 bronze badges 2- 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 Commented May 21, 2013 at 7:38
- It works for me. What is the error you got? – DanEEStar Commented May 21, 2013 at 8:22
1 Answer
Reset to default 13The 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;
});
}]);