您可以阅读服务器端代码中的配置部分并通过$http.get 检索它。这是最好的解决方案。
您应该在 MVC ApiController 中编写方法(也可以是 MVC Action)。比如:
[Route("settings/{key}")]
[HttpGet]
public HttpResponseMessage GetSetting(string key)
{
try
{
return Request.CreateResponse(HttpStatusCode.OK,
System.Configuration.ConfigurationManager.AppSettings[key]);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)
}
}
在您的角度服务中:
app.service('utilsService', utilsService);
utilsServcie.$inject = ['$http'];
function utilsServcie($http){
this.readServerProperty = function(key){
$http.get('/api/v1/settings/' + key).then(
function(response){
return response.data;
},
function(error){
alert(error.data.message);
}
);
}
}
您可以在需要的地方使用此服务,如下所示:
app.controller('MainController', MainController);
MainController.$inject = ['utilsServcie'];
function MainController(utilsServcie){
var vm = this;
utilsServcie.readServerProperty('SampleTemplate').then(function(path){
vm.path = path;
});
}
在你看来:
<div ng-controller="MainController as mc">
<a ng-href= "mc.path"> Edit Template</a>
</div>
嗯,这一切都是我凭记忆写的,所以可能会有一些小错误,但你可以理解这个想法。
这就是所有人。 :D