这个函数有太多的语句。 (41)

This function has too many statements. (41)

我有这个控制器

  .controller('ctrl', function($scope, $rootScope, $timeout, $alert, 
                               $location, $tooltip, $popover, BetSlipFactory, 
                               AccordionsFactory, AuthFactory, 
                               RiskWinCalculations) {...});

而且,由于 jshint:

,我收到了这个错误

line 10 col 44 This function has too many statements. (41)

那么,我应该怎么做才能避免呢?

这并不像@pankajparkar 之前所说的那样意味着管理不善的代码,这可能是因为你有这样的东西,让我们从我的一个项目中说出来:

  $scope.betLoader = false;
  $scope.showIfbetAlerts = true;
  $scope.displayStraight = true;
  $scope.displayParlay = true;
  $scope.displayIfBet = true;
  $scope.displayTeaser = true;
  $scope.displayPleaser = true;
  $scope.displayReverse = true;
  $scope.unavailableBet = false;
  $scope.subAccordion = false;
  $scope.betTypeShow = false;
  $scope.showStraight = true;

你可以这样做:

$scope.setInitialState = function() {
  $scope.betLoader = false;
  $scope.showIfbetAlerts = true;
  $scope.displayStraight = true;
  $scope.displayParlay = true;
  $scope.displayIfBet = true;
  $scope.displayTeaser = true;
  $scope.displayPleaser = true;
  $scope.displayReverse = true;
  $scope.unavailableBet = false;
  $scope.subAccordion = false;
  $scope.betTypeShow = false;
};
$scope.setInitialState();

这将解决它。

更新

让我解释一下:

不仅跟依赖有关,jslint在statements太多的时候会抛出这个错误,他之前在第十行说controller开始的地方,所以从那里分手,他应该有太多的statements,如果将所有这些语句放在 1 个函数中,这些语句将减少为 1 :)

消除该错误的最佳方法是编辑您的 jshint 设置以不显示它。

http://jshint.com/docs/options/#maxstatements

这是一个非常空洞的 jshint 警告,实际上没有任何意义。

通常情况下,出于多种原因,需要 4 或 5 个以上参数的函数不是一个好主意,但从技术上讲并没有错。在这种情况下,这些参数是 Angular 定义依赖关系的方式,所以应该不是问题。如果代码有效,我就不会担心了。

如果控制器需要更多语句,而您没有任何其他方法可以删除它,那么转到您的 .jshintrc 文件并像

一样编辑它
"maxstatements": 80, // or whatever number you want'

谢谢