myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
controller: 'CustomersController',
templateUrl: 'app/views/customers.html'
}).
when('/orders/:customerId', {
controller: 'OrdersController',
templateUrl: 'app/views/orders.html'
}).
otherwise({
redirectTo: '/'
});
}]);
<!------------Customer Controller------------------->
myApp.controller('CustomersController',['$scope','$http', function($scope,$http) {
$http.get('js/data.json').success(function(data) {
$scope.customers = data;
});
$scope.doSort = function(propName){
$scope.sortBy = 'name';
$scope.reverse = false;
$scope.doSort = function(propName){
$scope.sortBy = propName;
$scope.reverse = !$scope.reverse;
}
}
}]);
<!------------------- Orders Controller -------------------->
myApp.controller('OrdersController',['$scope','$http','$routeParams', function($scope,$http,$routeParams) {
$scope.customerId = $routeParams.customerId;
$scope.orders = null;
function init() {
//Seach the customer for customerID
for(var i=0,len=$scope.customers.length; i<len; i++){
if($scope.customers[i].id === parseInt(customerId)) {
$scope.orders = $scope.customers[i].orders;
break;
}
}
}
$http.get('js/data.json').success(function(data) {
$scope.customers = data;
});
init();
}]);
I have made a customers table in which their is an order link which displays the orders view.
However, the order view is not displaying any order but the customer view is working properly.
Can anyone tell me the error and data is coming from json file.
The error is console is cannot read property length of undefined