我正在为我的angularJS应用程序使用SonarQube。

我遇到了问题,因为Sonar检测到我的 Controller (它是一个函数),其行数太重要(超过100条),并且参数数(超过7条)也太重要了。

由于 Controller 是angularJS中的函数,因此对我来说,很容易超过这些数字是正常的。

最佳答案

如果使用controller as语法,则可以将 Controller 的功能添加到其原型(prototype)上,而不是拥有一个庞大的功能。例如

var MyController = function($http) {
    this.$http = $http;
}

MyController.$inject = ['$http'];
angular.module('myApp').controller('MyController', MyController);

MyController.prototype.someFunc = function() {
    return this.$http.get('something');
}

07-24 09:47
查看更多