我可以禁用参数模式匹配吗



Angular有一个功能,在某些情况下,它会尝试自动将参数与值匹配。IE:

angular.module("foo").controller(function($scope){
  //Angular will automatically fill in the value of $scope
}

这当然会在处理缩小时遇到问题,这就是我们使用数组语法的原因。

有可能禁用Angular的这个功能吗?我之所以这么问,是因为我最近遇到了一个错误,我的一些代码以未压缩的形式工作,但在压缩中失败了,根本原因是Angular意外地自动填充了一个变量。

由于我知道我所有的代码都是(或者至少应该)使用数组语法编写的,我想禁用这个功能,这样我就可以强制消除任何其他隐藏的错误。

是的,它被称为显式注入,启用严格依赖注入:

angular.module("foo").controller("SomeController", ["$scope", function($scope){
  //Angular will automatically fill in the value of $scope
}]);

或:

 function SomeController($scope){
      //Angular will automatically fill in the value of $scope
  }
  SomeController.$inject = ["$scope"];
  angular.module("foo").controller("SomeController", SomeController);

因为我知道我所有的代码都是(或者至少应该)使用我想禁用此功能的数组语法,以便可以强行清除任何其他隐藏的错误。

您可以使用严格依赖注入禁用自动参数匹配。请参阅Angular的生产指南以获取更多详细信息:

<div ng-app="myApp" ng-strict-di>
  <!-- your app here -->
</div>

或:

angular.bootstrap(document, ['myApp'], {
  strictDi: true
});

最新更新