0

I'm new to angular and busy with $routeProvider. I want to call a specific function in a controller and give $routeParams (i've found a workaround, but it's not desired. It involves creating a new controller).

https://docs.angularjs.org/api/ngRoute/provider/$routeProvider refers to resolve (but i don't want the function inside the $routeProvider).

This is the problem:

(function () {
angular
    .module('app', ['ngRoute', 'employee'])
    .config(function ($routeProvider) {
        $routeProvider.when('/settings/employees',
            {
                templateUrl: 'app/Employee/employee-table.html',
                controller: 'employeeController'
            })
        $routeProvider.when('/settings/employees/add/form',
            {
                templateUrl: 'app/Employee/employee-add-form.html',
                controller: 'employeeController'
            })
        $routeProvider.when('/settings/employees/edit/:employeeId/form',
            {
                templateUrl: 'app/Employee/employee-edit-form.html',
                controller: 'employeeController',
                //Call to 'editEmployee' method WITH $routeParams<<----------------------
            })
    });
})();

This is the controller:

(function () {
'use strict';

angular
    .module('employee')
    .controller('employeeController', function ($scope, $location, $routeParams, employeeFactory) {
        // Get all employees
        $scope.getAllEmployees = function () {
            return employeeFactory.getAllEmployees();
        }

        $scope.getEmployeeById = function (employeeId) {
            //TODO: IMPLEMENT:      NOT YET IMPLEMENTED!
        }

        // Add employee
        $scope.addEmployee = function (addEmployeeForm) {
            if (addEmployeeForm.$valid) {
                employeeFactory.addEmployee($scope.employee);
            }
        }

        // THIS IS THE FUNCTION TO CALL<<---------------------------------------------
        $scope.editEmployee = function () {
            console.log('reached');
            if ($routeParams) {
                console.log($routeParams);
            }
        }

        // Delete given employee
        $scope.deleteEmployee = function (employee) {
            employeeFactory.deleteEmployee(employee);
        }

        $scope.cancelForm = function () {
            $scope.go('/settings/employees');
        }

        // Reset the form
        $scope.resetForm = function () {
            $scope.employee = {}
        }

        // Route to path
        $scope.go = function (path) {
            console.log(path);
            $location.path(path);
        }
    });
});

Is there a way to call it like this?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • You can't inject a dependency into a function of a controller. Also, the `$routeParams` argument should be accessible to the `$scope.editEmployee` function because of how JavaScript deals with closures. The only thing that looks wrong with the code is that your IIFE is not executed, try adding brackets to the last line, e.g: `(function () { //... })();`. – Tom P. Apr 03 '16 at 15:01

1 Answers1

1

Why can't you check the existence of employeeId in $routeParams inside employeeController and call $scope.editEmployee method?

if($routeParams.employeeId){
  $scope.editEmployee();
}
Subash Selvaraj
  • 3,385
  • 1
  • 14
  • 17