gpt4 book ai didi

javascript - Controller 结构的差异

转载 作者:行者123 更新时间:2023-11-30 10:12:35 25 4
gpt4 key购买 nike

在 AngularJS 中,我见过以两种不同方式编码的 Controller 。

这样:

myApp.controller('SearchCtrl', ['$scope', '$location',
function($scope, $location) {
// code here
}]);

这样:

myApp.controller('SearchCtrl', function($scope, $location) {
// code here
});

两者有什么区别?

最佳答案

这只是允许它,以便在完成缩小后,Angular 将能够保持对实际依赖项的引用。这样如果

myApp.controller('SearchCtrl', function ($scope, $location, MyService) {
// code here
});

缩小后,它会变成这样:

myApp.controller('SearchCtrl',function(a,b,c){//code here});

而且由于没有 a、b 和 c 的提供者这样的东西,Angular 会废掉。当您添加方括号并将其缩小时,它会变成这样:

myApp.controller('SearchCtrl',['$scope', '$location', 'MyService',function(a,b,c){//code here}]);

并且 Angular 知道映射 $scope -> a、$location -> b 和 MyService -> c。

希望对您有所帮助!

关于javascript - Controller 结构的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25649061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com