我正在使用 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
我的 Angular 代码如下所示:
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 参数
非常感谢
最佳答案
我创建了一个 updated plunker here
状态看起来像这样:
.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})
检查所有 here
可以在此处找到 templateProvider“神奇”的详细信息:
- > Angular pass paramters in templateurl in stateprovider
- > Trying to Dynamically set a templateUrl in controller based on constant
扩展:
用 radio 我会调整它like this
<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>
还有go函数
$scope.Model = { selected: null };
$scope.go = function(index){
$state.go('guy_route2', {index: index}) ;}
});
查一下here
关于javascript - 我如何在angularjs UI-Router中为ng-repeat中的项目路由到不同的URL,需要修改ui-router中的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932624/