将 url 从 web.config 传递到 angularjs .html 视图
Pass url from web.config to angularjs .html view
我正在为此应用程序使用 Asp.net MVC 和 Angularjs。如何将 web.config 中键中的 url 传递给 angularjs 中的 link .html 视图?
Web.config :
<add key="SampleTemplate" value="C:\SampleTemplate" />
TemplateView.html:
<a ng-href= "WHAT SHOULD I WRITE HERE?"> Edit Template</a>
您可以读取服务器端代码中的配置部分并通过 $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)
}
}
并且在您的 angular 服务中:
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
我正在为此应用程序使用 Asp.net MVC 和 Angularjs。如何将 web.config 中键中的 url 传递给 angularjs 中的 link .html 视图?
Web.config :
<add key="SampleTemplate" value="C:\SampleTemplate" />
TemplateView.html:
<a ng-href= "WHAT SHOULD I WRITE HERE?"> Edit Template</a>
您可以读取服务器端代码中的配置部分并通过 $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)
}
}
并且在您的 angular 服务中:
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