Javascript 角度创建一个;“第三方模块”;,处理数据,还是获取数据?

Javascript 角度创建一个;“第三方模块”;,处理数据,还是获取数据?,javascript,angularjs,Javascript,Angularjs,所以我有一个模块,主要用作“第三方”模块。其目的是创建可以轻松适应其他角度应用的东西。到目前为止,我得到的是这样的东西 其他人的应用程序: var myApp = angular.module( 'myApp', ['thirdPartyModule'] ); angular.module('thirdPartyModule', []) .controller('thirdPartyController', function($scope, getDataForThirdPartyModule

所以我有一个模块,主要用作“第三方”模块。其目的是创建可以轻松适应其他角度应用的东西。到目前为止,我得到的是这样的东西

其他人的应用程序:

var myApp = angular.module( 'myApp', ['thirdPartyModule'] );
angular.module('thirdPartyModule', [])
.controller('thirdPartyController', function($scope, getDataForThirdPartyModule)
{
    getDataForThirdPartyModule.getData().then(function(data)
    {
        $scope.thirdPartyData = data;
        console.log($scope.thirdPartyData);
    });
})
.factory('getDataForThirdPartyModule', function($http)
{
    return {
        getData: function (callback) {
            return $http.get('/path/to/sample-data.json').then(function(response)
            {
                return response.data;
            });
        }
    }
});
我的第三方模块:

var myApp = angular.module( 'myApp', ['thirdPartyModule'] );
angular.module('thirdPartyModule', [])
.controller('thirdPartyController', function($scope, getDataForThirdPartyModule)
{
    getDataForThirdPartyModule.getData().then(function(data)
    {
        $scope.thirdPartyData = data;
        console.log($scope.thirdPartyData);
    });
})
.factory('getDataForThirdPartyModule', function($http)
{
    return {
        getData: function (callback) {
            return $http.get('/path/to/sample-data.json').then(function(response)
            {
                return response.data;
            });
        }
    }
});
目前,这适用于硬编码URL路径:

但我想知道的是什么是最好的方法?我的想法不是硬编码URL路径,而是动态的,由主应用程序或模块决定。例如,我的第一个想法是这样的

$http.get($scope.thirdpartyUrl);
但这不起作用,或者说似乎是对的。那么,做这种事情的好方法或正确方法是什么呢?

您可以使用:

然后,您可以在应用程序中配置提供商:

myApp.config(function(getDataForThirdPartyModuleProvider){
    getDataForThirdPartyModuleProvider.setUrl('/path/to/sample-data.json');
});

选中。

getData()
方法添加一个或多个参数,并根据在应答过程中传递的某种操作描述解析服务内的url,非常简单。我会尝试一下,然后再给你回复。嗯。。现在获取此错误
未知提供程序:$http
是否可以尝试包装$get函数:
['$http',function($http){…
?好的,我想我获取此错误是因为我忘了从
函数($http)中删除
$http
就像你建议的那样。但是我没有清除我的控制台。但是,我得到了这个错误。
未知提供程序:GetDataforThirdPartyModule提供程序
添加了一个plnkr示例