[英]How can I route to different URLS in angularjs UI-Router for items in ng-repeat, Need to modify state in ui-router
我正在使用UI-Router,而我的html如下所示:
<body ng-controller="myCtrl">
<h1>Hello Plunker!</h1>
<ul>
<li ng-repeat = "guy in guys">
<a ui-sref="person">{{guy}}{{$index+1}}</a>
</li>
</ul>
</body>
输出如下:
你好,笨蛋!
文件1
文件2
文件3
我的角度代码如下所示:
var app = angular.module('app', ['ui.router']);
app.controller('myCtrl', function($scope) {
$scope.val = "This is my Message..."
$scope.guys = ['File1','File2','File3']
});
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('person{something needs to be here}', {
url: "/route1",
templateUrl: "file1.html"
})
.state('person{something needs to be here}', {
url: "/route2",
templateUrl: "file2.html"
})
})
有人可以帮忙解决这里需要填充的内容吗,我的目标是单击File1应该打开file.html,单击file2应该打开file2.html
简而言之,我的问题是单击ng-repeat指令中重复的项目时如何打开不同的文件/模板/部分,以及如何在UI-Router
状态下指定url参数
非常感谢
州将看起来像这样:
.state('guy_route2', {
url: "/route/{index:int}",
templateProvider: function($templateRequest, $stateParams) {
var index = $stateParams.index + 1;
var templateName = 'file' + index + '.html';
return $templateRequest(templateName);
},
})
这将是身体:
<body ng-controller="myCtrl">
<ul>
<li ng-repeat = "guy in guys">
<a ui-sref="guy_route2({index: $index})">{{guy}}</a>
</li>
</ul>
<h3>Target for state</h3>
<div ui-view=""></div>
</body>
请参阅<div ui-view=""></div>
(我们各州的基本目标)。 和ui-sref:
ui-sref="guy_route2({index: $index})"
我们在其中传递状态名称'guy_route2'
,并且函数调用包含表示状态参数的对象({index: $index})
在这里检查所有
templateProvider的“魔术”详细信息可以在这里找到:
延伸:
随着无线电我会调整它像这样
<h3>radio</h3>
<ul>
<li ng-repeat="guy in guys">
<input type="radio"
value="{{$index}}"
ng-model="Model.selected"
ng-change="go(Model.selected)"
id="something{{$index}}"
/><label for="something{{$index}}">{{guy}}</label>
</li>
</ul>
和去功能
$scope.Model = { selected: null };
$scope.go = function(index){
$state.go('guy_route2', {index: index}) ;}
});
在这里检查
仅使用带有参数的一条路线:
.state("person", {
url: "/person/:file",
....
});
然后,在模板控制器中,使用$ stateParams获取file参数,使用$ http加载html内容,并将结果放入ng-bind-html tag属性。
正如这里评论所讨论的,我们甚至可以使用resolve
。
这可能是data.json
[
{
"id":1,
"name":"Someone 1"
},
{
"id":2,
"name":"Someone 2"
},
{
"id":3,
"name":"Someone 3"
}
]
这些状态def将使用该文件生成列表和详细信息。 父母有决心
.state('guys', {
url: "/route",
templateUrl:'guys.html',
controller:'listCtrl',
resolve: {
guys: ['$http', function($http){
return $http
.get("data.json")
.then(function(response){
return response.data;
})
}],
}
})
和具有通过传递的ID驱动的模板的孩子:
.state('guy', {
parent: 'guys',
url: "/:id",
controller:'MyCtrl',
templateProvider: function($templateRequest, $stateParams) {
var index = $stateParams.id;
var templateName = 'file' + index + '.html';
return $templateRequest(templateName);
},
})
那应该显示如何使用UI-Router和resolve
在这里检查
也可以看看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.