Cycle. JS Angularjs
-
It's a full-time code that walks forward with references, but I can't figure out how to do the same cycle to walk back.
$scope.nextId = function(id, a) {
for (var i = 0; i< a.length; i++) { if (id == a[i].id) if (i == a.length-1) { var next = '#/kitchens/'+a[0].id; } else { next = '#/kitchens/'+a[i+1].id;} } console.log('next',next); return next;
}
It's non-working, backwards.
$scope.prevId = function(id, a) {
for (var i = 0; i< a.length; i++) {
if (id == a[i].id)
if (i == a[0]) { var prev = '#/kitchens/'+(a.length-1).id; }
else { prev = '#/kitchens/'+a[i-1].id; console.log(a);}
}
return prev;
♪
It's the mark.
<a href="{{ prev }}" ng-click="prev = prevId(kitchen.id, kitchens)" style="color:green">NEXT</a>
<a href="{{ next }}" ng-click="next = nextId(kitchen.id, kitchens)" style="color:red">PREV</a>
It's a cotroller.
velesApp.controller('DetailCtrl',['$scope','$http', '$location', '$routeParams', function($scope, $http, $location, $routeParams) {
$scope.kitchenId = $routeParams.kitchenId;
var url = '/api/v1/kitchens/'+$routeParams.kitchenId;
$http.get(url).success(function(data, status, headers, config) {
$scope.kitchen = data;
});
$http.get('/api/v1/kitchens').success(function(data, status, headers, config) {
$scope.kitchens = data;
});
That's why I'm putting the binding in a separate kitchen, because I'm on the page taking out the object, not a bunch of kitchens. There's no ng-repeat in the code, binding out.
-
Flaw in the line
if (i == a[0])
This compares the index to the variable value, in this case the object, so the condition is always false.
Rapid truth--
i == 0
and inside the branch
instead(a.length-1).id
- Categorya[(a.length-1)].id
because a.length-1 It's a number and he doesn't have a field. id♪In fact, if viewed, the functions are small and can be combined to one function with a parameter
$scope.nextId = function(id, a, isNext) { var borderIndex=isNext? (a.length-1) : 0; var inc =isNext? 1 : -1;
for (var i = 0; i< a.length; i++) { if (id == a[i].id) { if (i == borderIndex) { var next = '#/kitchens/'+a[a.length-1-borderIndex].id; } else { next = '#/kitchens/'+a[i+inc].id; } //так как уже нашли, нет смысла бежать по циклу дальше console.log('next',next);// return next; } }
}