I'm trying to build out my first Angular app and having an issue with this:
(function() {
var ItemController = function($scope, $routeParams, $log, foodFactory) {
var foodId = $routeParams.foodId;
$scope.food = [];
$scope.item;
function init() {
foodFactory.getFoods()
.success(function(foods) {
$scope.food = foods;
})
.error(function(data, status, headers, config) {
//handle error
});
}
init();
for (var i = 0, len = $scope.food.length; i < len; i++) {
if (foodId == $scope.food[i].id) {
$scope.item = $scope.food[i];
}
}
};
angular.module('paleoApp')
.controller('ItemController', ['$scope', '$routeParams', '$log', 'foodFactory', ItemController]);
})();
The code properly sets $scope.food to the array of objects pulled from a JSON file by foodFactory, and the view also properly displays the array when {{ food }} is used. In order to get the proper object within the array, I tried looping through $scope.food to find the object with property 'id' that matches foodId in order to assign that object to $scope.item. However, the view won't display anything for {{ item }}.
What am I doing wrong?
Thanks.