2015-09-20 16 views
0

所以我是新來的JavaScript和angularjs。但是讓我困惑的是這個。爲什麼有兩種不同的角功能包裝方法,哪一種最好?

功能A

(function (angular) { 

    var AuthenticationService = function($http, $cookieStore, $rootScope, $timeout,UserService) { 

    } 

    AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout','UserService']; 
    angular.module("app.AuthenticationService").factory("AuthenticationService", AuthenticationService); 

})(angular); 

我開始與這一個和知道app.js一些測試的例子,我需要做這樣的事情:

(function(angular) {   
    angular.module("app.AuthenticationService", ['ngCookies']); 
    angular.module("app", ['ngRoute','ngResource',"app.AuthenticationService"]); 
}(angular)); 

功能B

(function() { 
    'use strict'; 

    angular 
     .module('app') 
     .factory('AuthenticationService', AuthenticationService); 

    AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService']; 
    function AuthenticationService($http, $cookieStore, $rootScope, $timeout, UserService) { 

    } 

})(); 

現在我用功能B的方法並不實用,但我很困惑哪一個更好的實用方法,爲什麼?

回答

0

這兩種方式都可以防止在window中定義AuthenticationService

但是作爲Function A的參數傳遞angular沒有意義,除了在estus的答案中提到的縮小。

功能B更好,因爲經過angular是不必要的,可以迷惑他人

+0

感謝您的解釋。 – Greg

相關問題