gpt4 book ai didi

javascript - 从 angularjs 中的 .properties 文件设置 $http url

转载 作者:行者123 更新时间:2023-12-03 12:26:00 25 4
gpt4 key购买 nike

我的应用程序中有一些服务。

loginApp.factory('urlService', function() {
return {
baseUrl : function(){
return 'http://localhost:8080/myAppName'
}
}
});

通过另一个服务使用此服务。

loginApp.factory('loginServices', function($http,urlService) {
return {
loginAuth : function(aut,resp){
return $http({
method: 'GET',
url: urlService.baseUrl()+'auth',
}).success(function(result) {
return result;
});
}
}
});

我想从应用程序根目录上存在的 .properties 文件配置 http://localhost:8080/myAppName

最佳答案

你可以做这样的事情

angular.module('app.properties', []).provider('properties', function() {

var resource;
$.ajax({
url: 'properties.json',
dataType: 'json',
async: false,
success: function(data) {
resource = data;
}
});
this.properties= resource;
this.$get = function() {
return this.properties;
};
});

然后在 Controller /服务中使用此提供程序来访问属性

 angular.module('loginApp', ['app.properties']).factory('urlService', function(properties) {
return {
baseUrl : function(){
return properties.baseUrl;
}
}
});

你的properties.json应该是这样的

{
"baseUrl" :"http://localhost:8080/myAppName"
}

注意:我使用 jquery ajax 来加载properties.json,因为它不应该是异步调用,所以我设置了 async: false

  $.ajax({
url: 'properties.json',
dataType: 'json',
**async: false**,
success: function(data) {
resource = data;
}
});

关于javascript - 从 angularjs 中的 .properties 文件设置 $http url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com