Angular 中的这两个函数有什么区别

what is difference between these two functions in Angular

本文关键字:函数 两个 什么 区别 Angular      更新时间:2023-09-26

>我有两个函数。 两者都在工作

app.controller("MainController", ["$scope","$http" ,function($scope,$http) {
}]);

    app.controller("MainController", function($scope,$http) {
});

哪一个最好用。 以及它们之间有什么区别。

谢谢

主要目的是缩小,但也有其他用途

当第二个控制器被缩小时,它将

app.controller("MainController", function(o,t) {
});

现在 angular 不知道什么是 o,t 服务... 当然,如果您选择使用它,则可以选择在缩小过程中不破坏变量。

使用第一种方法,缩小不会更改字符串值

app.controller("MainController", ["$scope","$http" ,function(o,t) {
}]);

第二种方法的另一个优点是,您可以在使用服务时随意命名它。例如,您可能有一个名为"dataStorageService"的很长的服务

app.controller("MainController", ["dataStorageService","$scope" ,function(dss,$scope) {
    dss.smallCodes();
}]);

因此,您可以使用非常棒的dss.smallCodes(),而不是使用dataStorageService.smallCodes();这意味着您可以使用有意义的单词来命名您的服务,而不必担心它太长。