如何将参数传递到angularJs中的工厂

How to pass a param to a factory in angularJs?

本文关键字:工厂 angularJs 参数传递      更新时间:2023-09-26

这是我的服务:

    app.factory('ApiStoreService', function ($q) {
        return $q(function (resolve, reject) {
            new SwaggerApi({
                discoveryUrl: "https://apiurl",
                apiKeyName : "apiKey",
                apiKey: "xxxxxxxxx",
                success: function () {
                    resolve(this);
                }
            });
        }) 
    });

我这样称呼它:

ApiStoreService.then(function (store) {// do something with store}

我想将硬编码的值作为参数传递。在角度上实现这一点的最佳方法是什么?

    app.factory('ApiStoreService', function ($q) {
    return{
        DoFunction: function(value){
            return $q(function (resolve, reject) {
                new SwaggerApi({
                    discoveryUrl: "https://apiurl",
                    apiKeyName : "apiKey",
                    apiKey: "xxxxxxxxx",
                    success: function () {
                        resolve(this);
                    }
                });
            })
        }
    }
});

并像这样使用:

var Obj = { name:'tim'};
ApiStoreService.DoFunction(Obj);

我想Angular Constants/Value会达到目的,请阅读以下要点,https://gist.github.com/demisx/9605099