将依赖项传递给模块 angular

Passing dependencies to a module angular

所以我对 angular 有点陌生,但对 javascript 不是。我正在使用其他人编写的应用程序,并试图在现有模块中创建一个新控制器。就依赖关系而言,控制器几乎彼此相同。我的问题是如何将相同的依赖项传递给两个控制器?看起来你可以像这样传递它们:

`angular.module('ApertureForm', ['dialogs.main','dialogs.default-translations','ui.bootstrap'])`

当我对两个模块执行此操作时,控制器 returns 出于某种原因未定义。那就是我卡住的地方。像上面的代码这样的依赖是怎么传入的呢?任何建议都会很棒。

angular.module('ApertureForm', ['dialogs.main','dialogs.default-translations','ui.bootstrap']) 告诉 AngularJS 初始化一个名为 ApertureForm 的 Angular 模块并加载其他 Angular 模块作为依赖 for模块.

要向控制器添加依赖项,您可以执行以下操作:

angular.module('ApertureForm').controller('ControllerName', function($scope, $location, $timeout) {...});

angular.module('ApertureForm') = 获取名为 ApertureForm 的模块。

然后创建一个名为 ControllerName 的控制器。闭包是您注入依赖项的地方。

要防止控制器的依赖项在缩小时重命名,您可以这样做:

angular
    .module('ApertureForm')
    .controller('ControllerName', ControllerName);


    ControllerName.$inject = ['$scope', '$location', '$timeout'];

    function ControllerName($scope, $location, $timeout) {...}

文档:https://docs.angularjs.org/guide/di

angular.module('ApertureForm',[ 中列出要注入到模块中的模块。您为每个模块列出一次。如果你想在你的模块之一中创建一个控制器,那么你可以使用:

var myApp = angular.module('ApertureForm');

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

这里的$scope是controller的依赖

您可以阅读更多相关信息here

AngularJS Controllers and Dependency Injection:

angular
    .module('ApertureForm', ['dialogs.main','dialogs.default-translations','ui.bootstrap'])
    .controller('FormController', ['$scope', '$http', function($scope, $http) { 
        // FormController code...
    }])
    .controller('WebinarController', ['$scope', '$http', function($scope, $http) {
        // WebinarController code...
    }]);

感谢大家的帮助!所有这些对于学习 angular 的结构以及如何正确使用该框架都非常有用。 @Yosvel Quintero 能够解释该模块的工作原理以及我收到错误的原因。当依赖项被传递到模块而不是控制器时,它们对所有控制器都可用,因为它自己的模块现在知道依赖项,这就是为什么我在尝试再次传递它们时不断出错。希望针对问题给出的答案也可以帮助某人学习 angular。谢谢大家!