我得到一个错误在我的angularJS,我不知道为什么。我试图使用这里描述的$inject属性注释方法:https://docs.angularjs.org/guide/di
我得到以下错误Uncaught TypeError: Cannot read property '$scope' of undefined在第44行
CarouselController.$inject['$scope'];
我的App.JS看起来像:
var bloxApp = angular.module('bloxApp', ['bloxApp.form', 'bloxApp.carousel']);
bloxApp.config(['$logProvider', function ($logProvider) {
$logProvider.debugEnabled(true);
}]);
我的blox-app.js看起来是这样的:(app.js先加载,blox-app.js紧随其后加载)
angular.module('bloxApp.common'[]);;angular.module('bloxApp').factory('lodash', ['$window', function (window) {
return window._;
}]);;(function () {
angular.module('bloxApp.form', []);
})();;(function () {
var FormController = function ($scope, $window, $http, _) {
$scope.choices = [{ id: 'choice1' }, { id: 'choice2' }, { id: 'choice3' }];
$scope.addNewPiece = function () {
var newItemNo = $scope.choices.length + 1;
$scope.choices.push({ 'id': 'choice' + newItemNo });
};
$scope.removePiece = function (int_id) {
var newItemNo = $scope.choices.id;
_.pull($scope.choices, _.find($scope.choices, { id: int_id }));
};
}
FormController.$inject = ['$scope', '$window', '$http', 'lodash'];
angular.module('bloxApp.form')
.controller('FormController', FormController);
})();
;;;(function () {
angular.module('bloxApp.carousel', []);
})();;(function () {
var CarouselController = function ($scope) {
$scope.slides = [
{
image: 'http://lorempixel.com/400/200/', text: 'hello'
},
{
image: 'http://lorempixel.com/400/200/food', text: 'hello'
},
{
image: 'http://lorempixel.com/400/200/sports', text: 'hello'
},
{
image: 'http://lorempixel.com/400/200/people', text: 'hello'
}
];
}
CarouselController.$inject['$scope'];
angular.module('bloxApp.carousel')
.controller('CarouselController', CarouselController);
})();
应该是CarouselController.$inject = ['$scope']
。
您错过了=
,因此它试图访问CarouselController
上$inject
属性上的$scope
属性,而不是将$inject
属性设置为['$scope']
。