我被告知我应该使用app.controller语法,以支持缩小.
重写示例(教程)示例,我发现我无法使其工作:
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';
}]);
Run Code Online (Sandbox Code Playgroud)
/* 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'];
Run Code Online (Sandbox Code Playgroud)
<body ng-app="myApp" ng-controller="PhoneListCtrl">
{{phones | json}}
</body> <!-- Outputs just an echo of …Run Code Online (Sandbox Code Playgroud) javascript dependency-injection controller angularjs angularjs-scope
只是学习依赖注入,我想我已经开始理解它了.
如果我走在正确的轨道上,请告诉我......
例如:这两个是等价的吗?
/* injection method */
function <controller_name>($scope) {}
<controller_name>.$inject = ['$scope'];
/* other method */
var app = angular.module('myApp');
app.controller(<controller_name>, function($scope) {});
Run Code Online (Sandbox Code Playgroud)