我的 angularjs 应用程序出现问题,在使用 ng-repeat 数组确定路由时,我的应用程序路由到了错误的页面。
数据看起来像这样,并在人员控制器中访问:
[
{
"name":"AJ lastname",
"img_name":"AJ_lastname",
"location":"Baltimore, Maryland",
"info":"stuff"
},
{
"name":"Albert lastname",
"img_name":"Albert_lastname",
"location":"Boston, Massachusetts",
"info":"stuff"
} // ... more data
]
html:(锚标记根据他们在数组中的索引链接到该人(我相信这可能是我需要更改以解决问题,但我不确定)
<ul class="main-list">
<li class="list-item fade" ng-repeat="student in students | filter:filter">
<a href="/#person/{{$index}}">
<img class="portrait listimg" ng-src="/images/{{student.img_name}}.jpg" alt="portrait of {{student.name}}">
<h2>{{student.name}}</h2>
<h4>{{student.location}}</h4>
</a>
</li>
</ul>
从角度路由:(带有'/person/:itemId'的路由路由到特定用户的特定页面,他们在数组中的索引决定了他们的id)
app.config(function ($routeProvider, $httpProvider) {
$routeProvider
.when('/list', {
templateUrl: './js/templates/list.html',
controller: 'ListController'
})
.when('/person/:itemId', {
templateUrl: './js/templates/person.html',
controller: 'PersonController'
})
.otherwise('/list');
});
这是动态页面的控制器。它对原始数组非常有效,但是一旦我尝试对数组进行排序,索引就不再对应于正确的学生。
app.controller('PersonController', function ($scope, $http, $routeParams) {
$scope.person = 'Someone\'s name';
$http.get('../js/students.json').success(function (data) {
$scope.allStudents = data;
$scope.studentId = $routeParams.itemId;
$scope.student = data[$scope.studentId];
});
所以功能问题是索引适用于大数据数组中的第一个学生。它似乎工作得很好,并且正确的数据填充了页面,但是当我使用 html/text 输入过滤列表时,原始索引在 html 端更新,它们与原始数组不对应。因此路由将它们发送到错误的页面。
即使对于过滤列表,我如何才能使路由工作?