2016-04-27 58 views
0

Angular有一个功能,在某些情况下,它会尝试自动将参数与值进行匹配。 IE:我可以禁用参数模式匹配吗?

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

这当然会遇到问题时处理缩小,这就是为什么我们使用数组语法。

是否可以禁用Angular的这个功能?我问,因为我最近有一个错误,其中一些代码是以未定义的形式工作的,但未能缩小,并且根本原因最终变成了一个意外由Angular自动填充的变量。

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

回答

2

是,这就是所谓明确注射严格依赖注入启用:

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); 

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

您可以禁用自动参数匹配使用严格依赖注入。见角的production guide获得进一步的细节:

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

...或:

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

算不上什么,他要求,因为他是问如何禁用参数匹配。 – mccainz

+0

@mccainz这就是明确的注入。它禁用自动参数匹配 –

+0

正如@mccainz提到的,我已经知道这个语法,并且在我的问题中提到了它。我正在寻找的是全球性的Angular选项,禁用不使用阵列的'简单'风格 –

相关问题