Man*_*h V 8 javascript ajax angularjs angular-ui-router
可通过API访问的服务器数据库中的slugs:
{slug: "john-smith",type: "user"}
{slug: "microsoft-technologies",type: "company"}
Run Code Online (Sandbox Code Playgroud)
场景1:用户视图和控制器:http:// localhost/john-smith
.state('user', {
url: '/:user',
templateUrl: 'partial-user.html',
controller: 'userCtrl'
})
Run Code Online (Sandbox Code Playgroud)
方案2:公司视图和控制器:http:// localhost/microsoft-technologies
.state('company', {
url: '/:company',
templateUrl: 'partial-company.html',
controller: 'companyCtrl'
})
Run Code Online (Sandbox Code Playgroud)
现在我想基于从API调用到服务器的slug做一个动态状态.
我写了一个虚构的代码.但我没有办法实现
// Example URL http://localhost/john-smith
.state('hybrid', {
// /john-smith
url: '/:slug',
templateUrl: function () {
return "partial-"+type+".html"
},
controllerProvider: function (rt) {
return type+'Controller'
},
resolove: {
type: function ($http, $stateParams) {
$http.get({
method: "GET",
url: "http://localhost/api/" + $stateParams.slug
}).success(function(response, status, headers, config){
//response = {slug: "john-smith",type: "user"}
return response.type
})
return
}
}
})
Run Code Online (Sandbox Code Playgroud)
Rad*_*ler 10
它来自类似的问题:AngularJS ui-router - 两个相同的路由组
在这种情况下,我确实理解你的目标,这将是调整后的状态定义(我只是跳过$ http和服务器响应部分,只是使用传递的参数):
.state('hybrid', {
// /john-smith
url: '/{slug:(?:john|user|company)}',
templateProvider: ['type', '$templateRequest',
function(type, templateRequest)
{
var tplName = "tpl.partial-" + type + ".html";
return templateRequest(tplName);
}
],
controllerProvider: ['type',
function(type)
{
return type + 'Controller';
}
],
resolve: {
type: ['$http', '$stateParams',
function($http, $stateParams) {
/*$http.get({
method: "GET",
url: "http://localhost/api/" + $stateParams.slug
}).success(function(response, status, headers, config){
//response = {slug: "john-smith",type: "user"}
return response.type
})*/
return $stateParams.slug
}
]
}
})
Run Code Online (Sandbox Code Playgroud)
resolove : {}变成了一个变化:resolve : {}.另一个是控制器def的固定(rt vs type).而且,我们从内置功能中获益(templateProvider并且$templateRequest 类似于:Angular ui.router reload parent templateProvider)
检查这里的行动
EXTEND,包括$ http部分(扩展的plunker)
让我们调整(对于plunker目的)服务器部分以返回此信息data.json:
{
"john-smith": {"type": "user"},
"lady-ann": {"type": "user"},
"microsoft-technologies" : {"type": "company" },
"big-company" : {"type": "company" },
"default": {"type" : "other" }
}
Run Code Online (Sandbox Code Playgroud)
这些链接:
<a href="#/john-smith">
<a href="#/lady-ann">
<a href="#/microsoft-technologies">
<a href="#/big-company">
<a href="#/other-unknown">
Run Code Online (Sandbox Code Playgroud)
将通过此调整状态def轻松管理:
.state('hybrid', {
// /john-smith
url: '/:slug',
templateProvider: ['type', '$templateRequest',
function(type, templateRequest)
{
var tplName = "tpl.partial-" + type + ".html";
return templateRequest(tplName);
}
],
controllerProvider: ['type',
function(type)
{
return type + 'Controller';
}
],
resolve: {
type: ['$http', '$stateParams',
function($http, $stateParams) {
return $http.get("data.json")
.then(function(response){
var theType = response.data[$stateParams.slug]
||response.data["default"]
return theType.type
})
}
]
}
})
Run Code Online (Sandbox Code Playgroud)
检查更新的东西在这里
| 归档时间: |
|
| 查看次数: |
8154 次 |
| 最近记录: |