Skip to content

Instantly share code, notes, and snippets.

@alexanderfrankel
Created December 28, 2015 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderfrankel/de168d2d5ee7d567583a to your computer and use it in GitHub Desktop.
Save alexanderfrankel/de168d2d5ee7d567583a to your computer and use it in GitHub Desktop.
testing-with-karms-my-controller.js
// 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