Angularjs 从状态路由中的$http请求加载模板url
我想从$http调用加载templateurlAngularjs 从状态路由中的$http请求加载模板url,angularjs,angular-ui-router,angular-ui,angular-template,Angularjs,Angular Ui Router,Angular Ui,Angular Template,我想从$http调用加载templateurl $stateProvider .state('home', { url: '/', templateUrl: will be served by $http call from server }); 请建议我将如何实现此功能。因为templateUrl也可以是一个您可以轻松实现的功能: $stateProvider.state('home', { templateUrl: function ($stat
$stateProvider
.state('home', {
url: '/',
templateUrl: will be served by $http call from server
});
请建议我将如何实现此功能。因为templateUrl也可以是一个您可以轻松实现的功能:
$stateProvider.state('home', {
templateUrl: function ($stateParams){
$http....success({
return <whatever> + '.html';
})
}
})
$stateProvider.state('home'){
templateUrl:函数($stateParams){
$http…成功({
返回+'.html';
})
}
})
作为templateUrl,您也可以轻松实现以下功能:
$stateProvider.state('home', {
templateUrl: function ($stateParams){
$http....success({
return <whatever> + '.html';
})
}
})
$stateProvider.state('home'){
templateUrl:函数($stateParams){
$http…成功({
返回+'.html';
})
}
})
我建议使用模板提供程序
。这可能会消耗任意数量的IoC参数(包括当前的$stateparms)。结合$templateRequest,我们还可以轻松地从服务器获取模板并将其缓存:
.state('myState',
{
...
// instead of templateUrl
templateProvider: ['$templateRequest', '$stateParams',
function($templateRequest,$stateParams){
var pathToTemplate = '....html';
// get the proper path from any IoC injected here
return $templateRequest(pathToTemplate);
}],
检查以下各项:
模板提供程序
。这可能会消耗任意数量的IoC参数(包括当前的$stateparms)。结合$templateRequest,我们还可以轻松地从服务器获取模板并将其缓存:
.state('myState',
{
...
// instead of templateUrl
templateProvider: ['$templateRequest', '$stateParams',
function($templateRequest,$stateParams){
var pathToTemplate = '....html';
// get the proper path from any IoC injected here
return $templateRequest(pathToTemplate);
}],
检查以下各项:
$stateProvider
.state('home', {
url: '/',
templateUrl: 'index.html',
resolve {
results: function($http){
return $http({method: 'GET', url: '/serverspi'})
.then (function (data) {
return process(data);
});
},
}
});
您将需要一个控制器来根据结果填充模板,并从模板绑定范围值
$stateProvider
.state('home', {
url: '/',
templateUrl: 'index.html',
resolve {
results: function($http){
return $http({method: 'GET', url: '/serverspi'})
.then (function (data) {
return process(data);
});
},
}
});
Angular以最优雅的方式完成了这项工作
.state('inbox', {
url: '/inbox',
templateUrl:'jspController/inboxRecord',
controller : 'inboxController'
})
在我的例子中,该url将以jsp页面响应inboxrecord.jsp,该页面将被呈现。Angular以最优雅的方式完成了该操作。如果您想调用后端控制器为templateurl请求jsp页面,只需像这样调用$http url即可
.state('inbox', {
url: '/inbox',
templateUrl:'jspController/inboxRecord',
controller : 'inboxController'
})
在我的案例inboxrecord.jsp中,该url用jsp页面响应,该页面将被呈现。我使用templateProvider()并检索到包含我的模板的JSON对象,操作如下:
templateProvider: function($http) {
return $http.get("http://example.com/returnJSONobject")
.then(function(res) {
return res.htmlTemplate;
});
},
我使用templateProvider()检索了一个包含我的模板的JSON对象,如下所示:
templateProvider: function($http) {
return $http.get("http://example.com/returnJSONobject")
.then(function(res) {
return res.htmlTemplate;
});
},
建议您阅读模板上的ui路由器文档:)建议您阅读模板上的ui路由器文档:)