I would like to implement my own custom object/classes in AngularJS. However, I'm getting stuck on the concept of using factories/services as custom objects - I understand that factories and services are there to provide the business logic of the app, but can I create multiple factories and services in an app? And if so, what exactly does the term "singleton"" (which many of the articles I've read have described services as) imply?
So for example, would this be the valid and/or the preferred way to go about creating a basic school object populated with student objects in AngularJS?
app.factory('schoolService',function(student){
var school = {};
school.students = [];
school.addStudent = function(){
var newStudent = new student();
this.students.push(newStudent);
}
return school;
}
app.service('student',function(){
//anyway to toss in a constructor here?
this.name = 'name';
this.getName = function(){
return this.name;
}
}
app.controller('schoolCtrl',function(schoolService){
schoolService.addStudent(); //obviously normally wouldn't do this, but for demonstration purposes
$scope.students = schoolService.students;
//expect $scope.students to equal an array of length 1, with one student object
}
Thanks in advance, and sorry for all the nested questions!