Below is an example of a controller:
myApp.controller('MedicineCtrl', ['medicineService', function (medicineService) { var vm = this; vm.medicineNames = medicineService.medicineNames; vm.addMedicine = function (input) { medicineService.addMedicine(input); }; vm.updateMedicine = function (input) { medicineService.updateMedicine(input); }; vm.deleteMedicine = function (id) { medicineService.deleteMedicine(id); }; }]);Of course, this is a simplified example however this should give us a picture of what a controller should be designed like.
For the service, it's recommended to have 'private' variables and functions needed and only expose publicly the ones that are necessary as service object's properties. Also whenever possible, try to design the functions to run asynchronously using AngularJS promise (will show an example in the next post). Below is an example of a service:
myApp.factory('medicineService', ['otherService1', 'otherService2', function (otherService1, otherService2) { // 'private' variables and functions var _medicines = []; var _privateVarOne; var _privateVarTwo; _addMedicine = function (input) { . . . // some business logic . . . }; _updateMedicine = function (input) { . . . // some business logic . . . }; _deleteMedicine = function (id) { . . . // some business logic . . . }; _anotherPrivateFunction = function () { . . . // some business logic . . . }; // only expose publicly the required members return { medicineNames: _medicineNames, addMedicine: _addMedicine, updateMedicine: _updateMedicine, deleteMedicine : _deleteMedicine } }]);
No comments:
Post a Comment