簡體   English   中英

在AngularJS中,最簡單的聲明工廠/服務的方法是什么?

[英]In AngularJS what is the cleanest way to declare a factory / service?

實施工廠/服務的正確/最干凈的方法是什么?

我遇到了幾種不同的方法,然后對自己進行了思考,這實際上是最好的方法...

首先,此方法:

(function(){
var github = function($http) {
    var getUser = function(username) {
        return $http.get("https://api.github.com/users/" + username)
            .then(function(response) {
                return response.data;
            });
    };

    var getRepos = function(user) {
        $http.get(user.repos_url)
            .then(function(response) {
                return response.data;
            });
    };

    return {
        getUser: getUser,
        getRepos: getRepos
    };
};
var module = angular.module("githubViewer");
module.factory("github", github);
}());

在最近的教程中,我正在閱讀使用的方法。 請注意,在底部,我要添加帶有名稱和功能的工廠。 函數調用包含$ http。 根據我對(new)angular如何與控制器一起使用的理解,為了使縮小正常工作,您需要首先將$ http作為字符串傳遞,然后將其用作參數。 該方法不起作用。

接下來,我將其重寫為如下所示:

(function(){
var github = function($http) {
    this.getUser = function(username) {
        return $http.get("https://api.github.com/users/" + username)
            .then(function(response) {
                return response.data;
            });
    };

    this.getRepos = function(user) {
        $http.get(user.repos_url)
            .then(function(response) {
                return response.data;
            });
    };

    return {
        getUser: getUser,
        getRepos: getRepos
    };
};
var module = angular.module("githubViewer");
module.factory("github", ["$http", github($http)]);
}());

我向工廠調用添加了一個參數數組。 運行代碼,它的工作原理相同。 我假設這將允許縮小? (如果它甚至有以前的問題)。 將“ $ http”添加到實際函數本身“ var github = function($ http)”是否多余? 因為我還刪除了“ $ http”以查看會發生什么,但它仍按預期運行。

最后我嘗試了這個:

(function(){
var module = angular.module("githubViewer");
module.factory("github",
    ["$http", function($http) {
        this.getUser = function(username) {
            return $http.get("https://api.github.com/users/" + username)
                .then(function(response) {
                    return response.data;
                });
        };

        this.getRepos = function(user) {
            $http.get(user.repos_url)
                .then(function(response) {
                    return response.data;
                });
        };

        return {
            getUser: getUser,
            getRepos: getRepos
        };
    };
]
}());

在我看來,這是最干凈的。 我沒有傳遞標記的函數,而是傳遞了一個匿名函數並提供了一個字符串來表示它,而不是傳遞一個標記函數。 我運行了代碼,它按預期工作。 我還添加了“ this”關鍵字,而不是將變量聲明為函數。 有或沒有“ this”仍然有效。

現在這是混亂...最好的方法是什么? 這些都不是最好的方法嗎? 我更喜歡使用this.name = function不是var name = function 在這方面最好的是什么? 使用起來實際上更具可讀性嗎

module.factory("github"["$http", function($http)

VS

module.factory("github", github);  

要么

module.factory("github", ["$http", github($http)]);"

除此之外,是否應該以相同的方式聲明控制器,服務,工廠,提供者?

我個人使用以下形式:

angular.module('myApp').controller('myController', [
    '$scope', '$state', '$http',
    function ($scope, $state, $http) {
        ...
    }
]);

當您這樣聲明服務/工廠/控制器等時,避免了全局范圍污染,您不必將聲明包裝在匿名函數中,而且它允許最小化。 正如喬在另一個答案中提到的,使用數組表示法是首選方法。

您在評論中提到了可讀性,我個人絕對沒有上述問題,也沒有任何抱怨。 在某些項目中,由於依賴性的原因,我使用了另一種形式,我將它們全部放在單獨的行上:

angular.module('myApp').controller('myController', [
    '$scope',
    '$state',
    '$http',
    function ($scope, $state, $http) {
        ...
    }
]);

這樣,很容易看到注入了什么,但是很難將其與函數的參數進行比較。 但這是優先事項。

我的看法是,代碼越少越好。 如果可以不使用匿名函數,為什么要使用它呢? 代碼越少,其可讀性就越高。 聲明一個單獨的函數並將其分配給您的定義也是一樣。 對我來說,這令人困惑。

有參與這個答案主觀性的一個很好的協議,但角的DI系統總是提示如圖所示你聲明依賴這里 和往常一樣,可能沒有最好的方法,但是有角度的方法。

在您的特定情況下,滾動到“內聯數組注釋”,它明確指出這是聲明服務的首選方式。

暫無
暫無

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

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