angular.module('app') .controller('HomeController', ['$scope', '$state', 'DataShareBusiness', 'WeeksBusiness', function($scope, $state, DataShareBusiness, WeeksBusiness) { $scope.DataShareBusiness = DataShareBusiness; $scope.courses = null; $scope.getSheetCount = function() { var sheetCount = function(group) { var c = (group.Groups == null || group.Groups.length == 0) ? 1 : 0; if (group.Groups != null) { group.Groups.forEach(function (group) { c += sheetCount(group); }); } return c; }; return sheetCount(DataShareBusiness.getCurrentGroupOrRoot()) }; $scope.$watch(function(){return DataShareBusiness.CurrentGroups}, function() { var group = DataShareBusiness.getCurrentGroup(); if (group != null && $scope.getSheetCount() < 5) { var promiseLoadCourses = { id: "promiseLoadCourses", groups: ["coursesView"] }; WeeksBusiness.getCurrentWeek({group_id: group.Id, type_id: group.Type}, promiseLoadCourses) .then(function(data) { $scope.courses = data; }); } else { $scope.courses = null; } }); }]);