gpt4 book ai didi

javascript - Angular 依赖注入(inject),哪个是更好的尝试,有什么区别?

转载 作者:行者123 更新时间:2023-11-27 23:40:57 25 4
gpt4 key购买 nike

我现在使用 Angular 一段时间了,当我阅读示例或教程时,我总是看到他们将服务注入(inject) Controller 的两种方法。

以下两种方法的主要区别是什么?

1.

.controller('MyController', ['myService', function(myService) {
}]);

2.

.controller('MyController', function(myService) {
});

我将服务包含在 Controller JS 文件的第一行:

angular.module('myApp.controllers', ['myApp.services']);

最佳答案

第一个样式是officially recommended .

但是第二种用法也有它自己的优点:

One advantage of this approach is that there's no array of names to keep in sync with the function parameters. You can also freely reorder dependencies.

您还可以使用 gulp-ng-annotate 等工具修复缩小问题。

由于 AngularJS 描述并支持这两种用法,我认为您可以自由选择其中任何一个,只需记住在整个项目中保留它即可。

关于javascript - Angular 依赖注入(inject),哪个是更好的尝试,有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666205/

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