簡體   English   中英

AngularJS中模塊依賴和服務依賴之間的區別

[英]Difference between module dependency and service dependency in AngularJS

我不明白模塊依賴和服務依賴之間有什么區別。 例如,

angular.module('components.users', [])
    .controller('UsersController', function(Users) {
        //...
}) 

angular.module('api.users', [])
  .factory('Users', function() {
        //...
});

我不明白為什么可以通過components.users控制器方法傳遞Users服務,因為component.users模塊不需要模塊。 (我認為api.users模塊應作為必需模塊之一以模塊方法傳遞)。

換一種說法...

模塊依賴性和服務依賴性之間有什么區別?

為什么即使不需要定義模塊也可以使用服務?

模塊之間的依賴性指定每個模塊(想象一個模塊是一組功能)如何需要其他模塊提供的某些功能。

服務依賴性是某種東西(控制器,服務等)需要特定服務才能工作的方式。

這些是相關的。 如果一個控制器需要另一個模塊中的服務,則可以在此處證明模塊依賴性和服務依賴性。 如果后面提到的服務在同一模塊內。 控制器的模塊不必依賴於另一個模塊。

想象一下:

  1. ModuleA
    • 功能1( 服務1
    • 功能2( 服務2
  2. ModuleB
    • 功能3( Service3
    • 功能4(服務4

如果Functionality1需要Functionality2 ,則可以使用它,而ModuleA無需導入ModuleB因為它們在同一模塊中。 現在,如果Functionality2需要Functionality4ModuleA需要進口ModuleB所以Functionality4可里面的“范圍”(用作字的一般意義上,不要與混淆帶來$scope )的ModuleA

AngularJS中的每個模塊都使用angular.module('name', [])聲明(請注意方括號)。 每個模塊(一旦創建)都可以使用angular.module('name') (注意沒有括號)。

您提供的代碼不起作用,因為UsersController (在components.users模塊中聲明)需要Users工廠(在api.users模塊中聲明),而components.users模塊不導入api.users

在下面看到(代碼顯然失敗了)

 angular.module('components.users', []) .controller('UsersController', function(Users) { console.log(Users); }) angular.module('api.users', []) .factory('Users', function() { return this; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <span ng-app="components.users" ng-controller="UsersController"></span> 

為了使此工作正常進行,您可以在相同的components.users模塊內聲明Users工廠,也可以在components.users內導入api.users模塊。

選項1:在相同的components.users模塊內聲明Users

 // create the module angular.module('components.users', []) .controller('UsersController', function(Users) { console.log(Users); }) // locate the module angular.module('components.users') .factory('Users', function() { return this }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <span ng-app="components.users" ng-controller="UsersController"></span> 

選項2:進口api.users內部模塊components.users

 // create the module with a dependency to `api.users` angular.module('components.users', ['api.users']) .controller('UsersController', function(Users) { console.log(Users); }) // create an independent module `api.users` angular.module('api.users', []) .factory('Users', function() { return this }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <span ng-app="components.users" ng-controller="UsersController"></span> 

您可以檢查您是否正在使用團隊中的現有代碼庫,他們是否已定義包含您的“用戶”服務的根或基礎“應用”模塊。 下面是一個示例,說明它如何在您的項目中起作用以及為什么能夠使用“用戶”服務:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <meta charset="utf-8">
</head>
<body>
    <div ng-controller="controller">
        {{ text }}
    </div>
    <script src="angular.js"></script>
    <script type="text/javascript">
    angular.module('services',[])
        .factory('service',function(){
            return {
                text: "this is a string value defined in a service."
            }
        })
    ;
    angular.module('controllers',[])
        /*
          Here the service "service" is being used however
          has not been specified in it's containing module.
        */
        .controller('controller',function($scope,service){
            $scope.text = service.text;
        });
    ;
    /*
      The above works because in some way all services and controllers
      have been defined. In this example by manually specifying each.
    */
    angular.module('app',['services','controllers']);
    </script>
</body>
</html>

每當您執行angular.module它就會變成一個單獨的模塊。

假設您住在公寓里,每所房子都是一個模塊。 每個房子都有自己的房間(服務)等。

因此,當您要訪問別人房屋的房間時,您需要請求其他房屋的許可。 在這種情況下,您要注入特定的模塊,然后只有您才能訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM