What I am attempting to do is import the employeeController to be able to use it for my application. Do I need to redefine the module empoyeesApp inside of the controller file? Is there a way to import and apply the file so that I don't have to copy the code back into the app.js file.
app.js
import angular from "angular";
angular.module("employeesApp", []);
employeeController.js
angular.module("employeesApp", [])
.controller("employeeController", function ($scope, employeeService) {
$scope.firedCount = employeeService.firedCount;
var promise = employeeService.getEmployees();
promise.then(list => {
$scope.employees = list;
});
$scope.addEmployee = function () {
var employee = {
"name": $scope.employee.name,
"street": $scope.employee.street,
"city": $scope.employee.city,
"state": $scope.employee.state,
"zip": $scope.employee.zip
};
employeeService.addEmployee(employee);
$scope.employee.name = '';
$scope.employee.street = '';
$scope.employee.city = '';
$scope.employee.state = '';
$scope.employee.zip = '';
$scope.employees = employeeService.getEmployees();
}
$scope.deleteEmployee = function (employee) {
employeeService.removeEmployee(employee);
$scope.employees = employeeService.getEmployees();
}
});
src
- app
- components
- employees
- views
- form.html
- table.html
- stats.html
- employeeController.js
- employeeService.js
- index.html
app.js
EmployeeController.js
EmployeeService.js
Declare controller and services in separate files and export the function so that you don't need to call
angular.module('moduleName').controller()orangular.module('moduleName').service()to attach them to the module.