正在调用 angularjs 配置块
calling angularjs config block
调用 angularjs 配置块时,在某些示例中我看到这样的代码 :-
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
....
}
在我看到的其他示例中:-
app.config(function($stateProvider, $urlRouterProvider) {
....
}
有什么区别?
第一个包括缩小的“注释”。
来自 Angularjs doc(关于缩小的注释)
Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.
We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified. There are two ways to provide these injection annotations.
所以基本上如果你想缩小你的代码,你必须使用第一种语法。
在第一种方法中,前 2 个是功能提供者的别名,您可以通过它的别名使用特定提供者这种方法用于缩小过程。
app.config(['sateP', 'urlRouterP', function($stateProvider, $urlRouterProvider) {
....
// you can use stateP
}
但是第二个你不能为你需要使用的提供者声明别名。
调用 angularjs 配置块时,在某些示例中我看到这样的代码 :-
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
....
}
在我看到的其他示例中:-
app.config(function($stateProvider, $urlRouterProvider) {
....
}
有什么区别?
第一个包括缩小的“注释”。
来自 Angularjs doc(关于缩小的注释)
Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.
We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified. There are two ways to provide these injection annotations.
所以基本上如果你想缩小你的代码,你必须使用第一种语法。
在第一种方法中,前 2 个是功能提供者的别名,您可以通过它的别名使用特定提供者这种方法用于缩小过程。
app.config(['sateP', 'urlRouterP', function($stateProvider, $urlRouterProvider) {
....
// you can use stateP
}
但是第二个你不能为你需要使用的提供者声明别名。