javascript - 我如何在angularjs UI-Router中为ng-repeat中的项目路由到不同的URL,需要修改ui-router中的状态

标签 javascript angularjs angular-ui-router angularjs-routing

我正在使用 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 参数

非常感谢

http://plnkr.co/edit/p6Qlzh7XjjeXUJ5I8Z8h?p=preview

最佳答案

我创建了一个 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“神奇”的详细信息:

扩展:

用 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/

相关文章:

javascript - 如何在部分输入中创建拆分?

javascript - AngularJs 应用程序中的下载损坏

javascript - 在使用内联数组注释和 Fluent API 定义的 Controller 上使用 ui-router 的解析

javascript - 如何在指令 Controller 中注入(inject)依赖项

java - 从服务器端 API 重定向时,Safari Angular URL 参数被忽略

javascript - 为什么不通过闭包定义这个变量?

javascript - 在 draft.js 中获取插入符位置(行号)

javascript - 删除数组中每个字符串的一部分

angularjs - rc 在 Angular 版本中是什么意思

angularjs - angular cli 无法创建 GitHub 存储库。错误 : 422 Unprocessable Entity