Created
December 28, 2015 20:55
-
-
Save alexanderfrankel/de168d2d5ee7d567583a to your computer and use it in GitHub Desktop.
testing-with-karms-my-controller.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the controller definition | |
// my-controller.js | |
var myApp = angular.module('myApp', []); | |
myApp.controller('MyController', ['$scope', '$http', function($scope, $http) { | |
$scope.grades = ['first', 'second', 'third'] | |
$scope.selectedGrade = null; | |
$scope.selectedStudents = []; | |
$scope.allStudents = []; | |
// select which grade you would like to see and load only students in that grade | |
$scope.selectGrade = function(grade) { | |
$scope.selectedGrade = grade; | |
_selectStudents(); | |
}; | |
// reset selected grade and selected students | |
$scope.deselectGrade = function() { | |
$scope.selectedGrade = null; | |
$scope.selectedStudents = []; | |
}; | |
// select only the students in the grade that has been selected | |
_selectStudents = function() { | |
for(i=0; i<$scope.allStudents.length; i++) { | |
if($scope.allStudents[i].grade === $scope.selectedGrade) { | |
$scope.selectedStudents.push($scope.allStudents[i]); | |
} | |
} | |
}; | |
// initially load in the students when the controller is initialized | |
$http.get('/students').then(function(response) { | |
$scope.allStudents = response.data.students; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment