If I leave the sort order unchanged, using routeParams.id to pull the correct object from my scope.data object works like a charm. As soon as the order changes, the routeParams.id changes and doesn't match up with the order of objects within $scope.data.
$scope.cars = {
[ {"make":"koenigsegg", "model":"agera"}, { "make":"pagani", "model":"huayra"},
{"make":noble, "model":"m600"}, {"make":"bugatti", "model":"veyron"} ]
}
// car list page
<li ng-repeat="(id, car) in cars">
<a href="#/{{id}}>{{car.model}}</a>
</li>
// Get data for individual car based on URL
// This is the key piece I'm having trouble with
controller.myCtrl = function($scope, $routeParams) {
$scope.car = $scope.cars[$routeParams.id];
};
// specific car page
<p>{{car.make}}: {{car.model}}</p>
So clicking "Agera" with id 0 will open /#/0 and the binding will correspond to $scope.cars[0], showing the data for the Agera. But if the sort order changes to say, "make", putting the Bugatti first, now if you click the "Veyron" link an id of 0 will again be passed but since the data object hasn't changed the 0 still maps to the "Agera."
Ideally, I'd like to just use my own $routeParams so that the URL is more explicit:
$routeProvider.when('/:make/:model, { ... });
<a href="#/{{car.make}}/{{car.model}}">{{car.model}}</a>
But now I have no way of telling my data array which car object to use. So how do I use $routeParams to connect to the correct "car" object in my cars array without using the array order id?
Or how can I keep the ids fixed to their objects so that they aren't attached to the sort order?
== Edit ==
Ok, I've changed my scope to explicitly call out each item in the object:
$scope.car = { make: routeParams.make, model: routeParams.model, ... };
So that works. Seems horribly inefficient though for big objects. Plus it doesn't work if that data isn't in the URL. Is there a better way?