在templateurl角传中paramters stateprovider [英] Angular pass paramters in templateurl in stateprovider
问题描述
我使用的是 $ stateprovider
从如下的角UI路由器在我的角度应用程序:
I am using a $stateprovider
as follows from angular-ui-router in my angular application:
.state('order', {
url: "/order/:id",
templateUrl: "/myapp/order"
})
在上面的场景中,我们传递一个 ID
到控制器,我们可以将其称为 UI-SREF =命令({ID :1234})
。
In the above scenario, we are passing an id
to the controller and we can call it as ui-sref="order({id: 1234})"
.
但现在我想通过不使用控制器进行到后端直接调用,并通过上述如下:
But now i want to make a direct call to the backend by not using a controller and pass the above as follows:
.state('order', {
url: "/order",
templateUrl: "/myapp/order/:id"
})
但很明显,我的语法是错在这里。
我如何做到这一点的情况?
But obviously my syntax is wrong here. How do i achieve this scenario?
推荐答案
创建工作例如的。据adjsuting thiw Q&安培;答:
I created working example. It is adjsuting thiw Q & A:
让我们的模板模板A.html
和模板B.html
在我们的例子。
DEF与国家的 templateProvider
是这样的
Let's have templates template-A.html
and template-B.html
in our example.
The state def with templateProvider
would look like this
$stateProvider
.state('order', {
url: '/order/:id',
templateProvider: function($http, $templateCache, $stateParams) {
var id = $stateParams.id || 'A';
var templateName = 'template-' + id + '.html';
var tpl = $templateCache.get(templateName);
if (tpl) {
return tpl;
}
return $http
.get(templateName)
.then(function(response) {
tpl = response.data
$templateCache.put(templateName, tpl);
return tpl;
});
},
controller: function($state) {}
});
现在我们可以这样调用
And now we can call it like this
<a href="#/order/A">
<a href="#/order/B">
检查一下这里
和更重要的是,随着angularjs的最新版本,我们甚至可以让它超级简单:
And what's more, with latest version of angularjs we can even make it super simple:
$templateRequest
.state('order', {
url: '/order/:id',
templateProvider: function($templateRequest, $stateParams) {
var id = $stateParams.id || 'A';
var templateName = 'template-' + id + '.html';
return $templateRequest(templateName);
},
controller: function($state) {}
});
这篇关于在templateurl角传中paramters stateprovider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!